一.常见的排序算法
冒泡排序:
public class maoPao {
public static void main(String[] args) {
int a[]= {7,8,5,6,10};
int b[]=maoPao(a);
for (int i : b) {
System.out.println(i);
}
}
public static int[] maoPao(int[] a) {
int temp;
for (int i = 0; i < a.length-1; i++) {
for (int j = 0; j < a.length-i-1; j++) {
if (a[j]>=a[j+1]) {
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
System.out.println(a[j]);
}
}
return a;
}
}
选择排序
//选择排序==>比较相邻的元素。如果第一个比第二个大,就交换他们两个。
public class PaiXu {
public static void main(String[] args) {
int a[]= {7,8,5,6,10};
int b[]=xuanZe(a);
for (int i : b) {
System.out.println(i);
}
}
public static int[] xuanZe(int[] a) {
int temp;//设置中间值
for (int i = 0; i < a.length-1; i++) {
for (int j = i+1; j < a.length; j++) {
if (a[i]>=a[j]) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return a;
}
}
3.数组反转
方法一:直接将数组中的最前面的数与最后面的数依次交换
public class Paixu1 {
public static void main(String[] args) {
char[] a= {'a','1','我','k','2'};
int len=a.length;
for(int i=0;i<len/2;i++){
char tmp = a[i];
a[i] = a[len-1-i];
a[len-1-i] = tmp;
}
for (char c : a) {
System.out.println(c);
}
}
}
方法二:直接加入list集合中,利用集合自带的reserve方法进行反转
public class Paixu1 {
public static void main(String[] args) {
char[] a= {'a','1','我','k','2'};
List list=new ArrayList<>();
for (Object o : a) {
list.add(o);
}
Collections.reverse(list);
System.out.println(list);
}
}
4.单例模式
饿汉模式:
public class Singleton{
private static Singleton singleton=new Singleton();
private Singleton() {
}
private static Singleton getSingleton() {
return singleton;
}
}
懒汉模式
class Singleton1{
private static Singleton1 singleton1;
private Singleton1() {
}
private static Singleton1 getSingleton1() {
if (singleton1==null) {
singleton1=new Singleton1();
}
return singleton1;
}
}
五.统计一个字符串中,中文,英文与数字的个数
public class StringTrim {
public static void main(String[] args) {
String str="ABCabc我爱你123";
int countNum=0;//统计数字的个数
int countChinese=0;//统计中文字符个数
int countEnglish=0;//统计英文字符的个数
//1.首先将str转为字符数组
char[] c=str.toCharArray();
//2.遍历字节数组进行判断
for (char d : c) {
if (d>='0'&&d<='9') {
countNum++;
}
else if ((d>='a'&&d<='z')||(d>='A'&&d<='Z')) {
countEnglish++;
}
else {
countChinese++;
}
}
System.out.println("中文的字符个数:"+countChinese);
System.out.println("数字的字符个数:"+countNum);
System.out.println("英文的个数:"+countEnglish);
}
}
//时,把值按照指定顺序输出来。
//例:n=1237
//则输出为:
//1237,
//2474,
//4948,
//9896,
//9896,
//4948,
//2474,
//1237,
public class Test1 {
public static void main(String[] args) {
System.out.println("请输入一个小于5000的数字");
Scanner scanner=new Scanner(System.in);
int num=scanner.nextInt();
getNum(num);
}
public static int getNum(int num) {
if (num/2<=5000) {
System.out.println(num);
getNum(num*2);
System.out.println(num);
}
return num;
}
public static void getNum1(int num) {
}
}
六.向上转型与向下转型
public class Test2 {
public static void main(String[] args) {
Person person=new Student();//向上转型
person.work();//输出学生在学习
person.play();//这个方法是获取不到的
Student student=(Student) person;//向下转型
student.play();//输出学生在玩
student.work();//输出学生在学习
}
}
class Person{
String type;//人的种类
void work() {
System.out.println("人类在工作");
}
}
class Student extends Person{
String name;//学生姓名
void work() {
System.out.println("学生在学习");//覆盖父类的方法
}
void play() {
System.out.println("学生在玩");//定义自身的方法
}
}
七.分解质因数
public class Test3 {
public static void main(String[] args) {
getNum(1000);
}
public static void getNum(int n) {
int k=2;
while (k<=n) {
if (k==n) {
System.out.println(k);
break;
}
else {
if (n%k==0) {
System.out.println(k+"*");
n=n/k;
}else {
k++;
}
}
}
}
}
八.修改参数的 编码
public class Web {
public static void main(String[] args) throws UnsupportedEncodingException {
String userName="123";
String userName1=new String(userName.getBytes("iso-8851"), "utf-8");
}
}
九.
//在一个十位的随机有序数组中插入一个数,重新排序
public class Test6 {
public static void main(String[] args) {
int a[]= {2,3,7,10};
System.out.println("请输入一个数字");
Scanner scanner=new Scanner(System.in);
int num=scanner.nextInt();
//步骤一:先找到num插入到数组a中的位置
int index=0;
for (; index < a.length-1; index++) {
if (num>=a[a.length-1]) {//若为最大则插入到最后一位
index=a.length;
break;
}
if (num<a[index]) {
System.out.println("位置为"+index);
break;
}
}
//步骤二:新建数组b,将插入位置之前的赋值给新数组
int b[]=new int[a.length+1];
for (int i = 0; i <=index; i++) {
if (i==index) {
b[i]=num;
}else {
b[i]=a[i];
}
}
//步骤三:将插入位置之后的赋值给新数组
for (int i = index; i < a.length; i++) {
b[i+1]=a[i];
}
for (int x : b) {
System.out.println(x);
}
}
}
十.递归遍历文件夹
public class Test7 {
public static void main(String[] args) {
File file=new File("E:/");//要遍历的文件夹
if (file!=null) {
list(file);
}
}
static void list(File f) {
if (f.isDirectory()) {
File[] files=f.listFiles();
for (File file : files) {
list(file);//递归调用
}
}else {
System.out.println(f);
}
}
}
十一:换行输出
public class Test8 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("请输入起始值 ");
int start_num=scanner.nextInt();
System.out.print("请输入终点值 ");
int end_num=scanner.nextInt();
System.out.print("请输入一行显示多少个数 ");
int n=scanner.nextInt();
fun( start_num,end_num, n );
}
public static void fun(int start_num,int end_num,int n ){
for (int i = start_num; i <= end_num; i++) {
System.out.print(i + " ");
if (i % n != 0)
continue;
System.out.println("");// 当数字一行不整除的时候不换行,持续打入,
// 当整除的时候,换行并且跳出到外循环继续
}
}
}
十二:打印三角形
public class Test9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<=5;i++){
for(int j=1;j<=5-i;j++){
System.out.print(" ");
}
for(int m=1;m<=i*2-1;m++){
System.out.print("*");
}
System.out.println();
}
}
}
public class Test10 {
public static void main(String[] args) {
for (int i = 0; i<5; i++ )
{
for (int j = 0; j<i; j++)
{
System.out.print(" ");
}
for (int k = 0; k<2*(5-i)-1; k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
十三:判断是否是素数
就是在所有比 1 大的整数中,除了 1 和它本身以外,不再有别的 约数,这种整数叫做质数,质数又叫做素数
求1到100之间的素数
public class Test11 {
public static void main(String[] args) {
int count = 0;//统计素数的个数
for (int i = 0; i <100; i++) {
boolean b=false;//定义一个标识符
for (int j = 2; j <Math.sqrt(i); j++) {
if (i%j==0) {
b=false;//有除得尽的代表不是素数所以为false
break;
}else {
b=true;
}
}
if (b==true) {
count++;
System.out.println(i);
}
}
System.out.println("素数的个数是"+count);
}
}
十四.去除数组中的重复元素
//将数组中相同的元素去除
public class Test12 {
public static void main(String[] args) {
int a[]= {1,2,5,5,3,3,10};
int count=0;//统计重复元素的个数
List list=new ArrayList<>();
for (int i = 0; i < a.length-1; i++) {
list.add(a[i]);
}
Set set=new HashSet<>(list);
Object a1[]=set.toArray();
for (int i = 0; i < a1.length; i++) {
System.out.println(a1[i]);
}
}
}
十五.位数反转
public class Test13 {
public static void main(String[] args) {
//1234===>4321
System.out.println("请输入一个整数");
Scanner scanner=new Scanner(System.in);
int num=scanner.nextInt();
System.out.println("反转后的数是");
int result = 0;
while (true) {
int n = num % 10;
result = result * 10 + n;
num = num / 10;
if (num == 0) {
break;
}
}
System.out.println(result);
}
}
public class Test14 {
public static void main(String[] args) {
String a="我爱中国";
//步骤一:将字符串转为字符数组
char b[]=a.toCharArray();
//步骤二.将数组转为集合.利用集合类的工具进行反转
List list=new ArrayList<>();
for (int i = 0; i < b.length; i++) {
list.add(b[i]);
}
Collections.reverse(list);
System.out.println(list.indexOf(1));
StringBuffer stb=new StringBuffer();
for (int i = 0; i < b.length; i++) {
stb.append(list.get(i));
}
System.out.println(stb);
}
}
十六.查找字符串中某字符串的个数
public class Test16 {
public static void main(String[] args) {
String string="abcabc123ababab";
String str="ab";
int count=0;
while (string.contains(str)) {
count++;
string=string.substring(string.indexOf(str)+str.length());
}
System.out.println(count);
}
}
十七。如何求出某个字符里各个字符的个数
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;
//题目:一个字符串中可能包含a~z中的 多个字符,如有重复,如String data="aavzjksdhdksahdkas"
//求出现次数最多的那个字母及次数,如有多个重复则都求出
public class Test1 {
public static void main(String[] args) {
String data="aaacccdddddooffggasdae";
char[] a=data.toCharArray();//第一步:将字符串转为字符数组
ArrayList<String> list=new ArrayList<>();//创建一个list集合
TreeSet<String> set=new TreeSet<>();//创建一个set集合
int num=0;
for (int i = 0; i < a.length; i++) {
list.add(String.valueOf(a[i]));
set.add(String.valueOf(a[i]));
}
Collections.sort(list);
for (int i = 0; i < list.size()-1; i++) {
while (!list.get(i).equals(list.get(i+1))) {
System.out.println(list.get(i)+"的个数"+(i+1-list.indexOf(list.get(i))));
break;
}
}
}
}