总结1118

总结

java自带的数组赋值的方法

Arrays.copyOf(原来的数组名,新的数组长度);

Arrays.sort(数组名);将数组从小到大排序

 

冒泡排序:

外层循环长度是数组长度-1

内层循环长度是数组长度-1-i; i是外层的循环次数

里面判断两两交换

int[] nums = { 11, 32, 44, 52, 64, 33, 22, 44, 75, 53 };
int temp = 0;
for (int i = 0; i < nums.length - 1; i++) {
for (int j = 0; j < nums.length - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
for (int i : nums) {
System.out.println(i);
}

选择排序:
int[] nums = { 11, 32, 44, 52, 64, 33, 22, 44, 75, 53 };

int temp;

for(int i = 0; i<nums.length;i++){
int k = i
for(int j=i;j<nums.length;j++){
if(nums[j] < nums[k]){
k = j;
}
}
temp = nums[i];
nums[i] = nums[k];
nums[k] = temp;
}

插入排序:
int[] nums = { 11, 32, 44, 52, 64, 33, 22, 44, 75, 53 };
int temp;
int j;
for(int i=0; i<nums.length; i++){
   temp = nums[i];
   for( j=i; j>0; j--){
       if(temp < nums[j-1]){
           nums[j] = nums[j-1];
      }else{
           break;
      }
  }
   nums[j] = temp;
}

二维数组:

数组长度: 数组名.length (行数); 数组名[i].length (列数);

二维数组特殊情况:

//这样写相当于只是定义了行数,每一行的列数,并没有定死
int[][] nums = new int[2][];
nums[0] = new int[3];
nums[0][0] = 10;
nums[0][1] = 20;
nums[0][2] = 30;

nums[1] = new int[2];
nums[1][0] = 99;
nums[1][1] = 999;

for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.print(nums[i][j] + "\t");
}
System.out.println();
}
方法:

现阶段定义方法的格式:

public static 放回值 方法名(参数){ 方法块}

字符串:

字符串就是字符类型所组成的数组,

字符串转化成字符数组 toCharArray()

char [] 数组名 = 字符串名.toCharArray();

字符串.toLwerCase(); 把所有字母变成小写

字符串.toUpperCase(); 把所有字母变成大写

posted @ 2019-11-28 11:32  hcjk  阅读(214)  评论(0)    收藏  举报