JAVASE (eight) arrays: one-dimensional array, two-dimensional arrays, dynamic arrays, static arrays

1, one-dimensional arrays

1.1 to declare and initialize an array
declaration ways:

String str[]; //不建议使用
String[] str2;

Static Initialization: Initialization and assignment simultaneously

String[] str = new String[]{“aa”,”bb”}
String[] str2 = {“aa”,”bb”} //声明和初始化不可以分开

Dynamic Initialization: Initialization and assignment separately

String[] str = new String[5];//5代表是数组的长度

Description: Whether it is static or dynamic initialization initialization, the array is created if successful, the length of the array immutable.

1.2 calls the elements in the array

String[] names = new String[5];
  1. An array index (the subscript: 0 starting from
  2.  Value: String str = names [0]
  3.  Assignment: names [0] = "aa";

1.3 Properties of the array - length (length of the array)

names.length;

1.4 traversing the array

for (int i = 0; i < citys.length; i++) {
            System.out.println(citys[i]);
}

The default value of 1.5 array elements    

The default value is the same as the array elements (depends on the specific element type) of defaults and member variables.

1.6 Analysis of memory arrays

2, two-dimensional array

2.1 declare and initialize an array of
arrays statement:

String[][] names;
String[] names[]; //不建议
String names[][]; //不建议

Static Initialization:

names = new String[][]{{“aa”,”bb”},{“cc”,”dd”}}
String[][] names = {{“aa”,”bb”},{“cc”,”dd”}}

Dynamic Initialization:

String[][] names = new String[3][2];
//3是二维数组的长度。2是二维数组元素的长度
String[][] names = new String[2][];
names[0] = new String[2];
names[1] = new String[3];

2.2 Invoking the elements in the array

String name = names[1][0];
names[0][0] = “aa”; //赋值

2.3 Properties of the array: length

String[][] s = new String[3][2];
s.length //数组的长度
s[0].length //二维数组中0位置上元素的长度

2.4 traversing the array

// 5.遍历二维数组的元素
        for (int i = 0; i < aa.length; i++) {

            String[] str = aa[i]; // 二维数组中的元素 for (int j = 0; j < str.length; j++) { System.out.println(str[j]); // 遍历的是二维数组中元素(一维数组)的元素 } } //下面的为标准的二维数组元素的遍历方式 for (int i = 0; i < aa.length; i++) { for (int j = 0; j < aa[i].length; j++) { System.out.println(aa[i][j]); // 遍历的是二维数组中元素(一维数组)的元素 } } 

The default value of 2.5 two-dimensional array of elements

The default value is 1. The two-dimensional array element is null

2.6 Analysis of two-dimensional array of memory

3, an array of common algorithm

        // 求数组元素的最大值
	public int getMaxNumber(int[] numbers) { int max = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (max < numbers[i]) { max = numbers[i]; } } return max; } // 求数组元素的最小值 public int getMinNumber(int[] numbers) { int min = numbers[0]; for (int i = 1; i < numbers.length; i++) { if (min > numbers[i]) { min = numbers[i]; } } return min; } // 求数组元素的总和 public int getSum(int[] numbers) { int sum = 0; for (int i = 0; i < numbers.length; i++) { sum += numbers[i]; } return sum; } // 求数组元素的平均值 public int getAvg(int[] numbers) { int sum = getSum(numbers); return sum / numbers.length; } // 数组的复制 public int[] getCopyArray(int[] numbers) { int[] copyNumber = new int[numbers.length]; for (int i = 0; i < numbers.length; i++) { copyNumber[i] = numbers[i]; } return copyNumber; } // 数组的反转 public void reverse(int[] numbers) { // 数组的反转 System.out.println(); for (int i = 0, j = numbers.length - 1; i < numbers.length / 2; i++, j--) { int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; } } //线性查找 public int searchNumber(int[] numbers,int findNumber){ int index = -1; //查找的数据所在的索引值。 for (int i = 0; i < numbers.length; i++) { if(findNumber == numbers[i]){ index = i; break; } } return index; } //排序 //boo 为false从小到大,如果为true 从大到小 public void sort(int[] numbers,boolean boo){ if(boo){ for (int i = 0; i < numbers.length - 1; i++) { //循环几轮 for (int j = 0; j < numbers.length - 1 - i; j++) { // 循环几次 if(numbers[j] < numbers[j + 1]){ int temp = numbers[j]; numbers[j] = numbers[j + 1]; numbers[j + 1] = temp; } } } }else{ for (int i = 0; i < numbers.length - 1; i++) { //循环几轮 for (int j = 0; j < numbers.length - 1 - i; j++) { // 循环几次 if(numbers[j] > numbers[j + 1]){ int temp = numbers[j]; numbers[j] = numbers[j + 1]; numbers[j + 1] = temp; } } } } }

Bubble algorithm:

        冒泡排序:
        for (int i = 0; i < numbers.length - 1; i++) { //循环几轮 for (int j = 0; j < numbers.length - 1 - i; j++) { // 循环几次 if(numbers[j] < numbers[j + 1]){ int temp = numbers[j]; numbers[j] = numbers[j + 1]; numbers[j + 1] = temp; } } }

4, using the tools of Arrays

            

5, an array of common exceptions

The first: null pointer exception - a NullPointerException
second: the subscript bounds - IndexOutOfBoundsException

Code:

    public static void main(String[] args) { int[] numbers = new int[2]; //下角标越界 // numbers[2] = 5; // System.out.println(numbers[2]); // numbers[-1] = 5; //下角标越界 // System.out.println(numbers[2]);//下角标越界 //空指针异常 String[] persons = new String[2]; //将字符串中的字符全部转成大写 System.out.println("aaaa".toUpperCase()); //下面会报空指针异常,因为persons[0]中的元素是null // System.out.println(persons[0].toUpperCase()); String[][] persons2 = new String[2][]; // System.out.println(persons[0].charAt(0)); //空指针异常 String[] names = {"aa","bb"}; System.out.println(names[0].toUpperCase()); //其它常见的空指针异常 ArrayException exception = new ArrayException(); Person person = null; // person = new Person(); System.out.println(exception); System.out.println(person); exception.personShow(person); } public void personShow(Person person){ person.show(); }

6, a variable number of parameter

Format: method name (variable type ... Variable name)
Description:

    1. The number of parameters can be modified may be one or more 0
    2. If a method, in addition there are other parameters can be modified parameters, the parameters can be modified last.
    3. Only one method of a deformable parameter
    4. Method using variable parameters and parameter method using an array portion is the same

Guess you like

Origin www.cnblogs.com/wushaopei/p/12204097.html