JAVASE(八) 数组: 一维数组、二维数组、动态数组、静态数组

个人博客网:https://wushaopei.github.io/    (你想要这里多有)

1、一维数组

1.1 数组的声明和初始化
声明方式:

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

静态初始化:初始化和赋值同时进行

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

动态初始化:初始化和赋值分开进行

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

说明: 无论是静态初始化还是动态初始化,数组一旦创建成功,数组的长度不可变。

1.2 调用数组中的元素

String[] names = new String[5];
  1. 数组的索引(下角标:是从0开始的
  2.  取值 :String str = names[0]
  3.  赋值 : names[0] = “aa”;

1.3 数组的属性 – length (数组的长度)

names.length;

1.4 数组的遍历

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

1.5 数组元素的默认值    

数组元素(要看具体的元素类型)的默认值和成员变量的默认值相同.

1.6 数组的内存分析

2、二维数组

2.1数组的声明和初始化
数组的声明:

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

静态初始化:

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

动态初始化:

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调用数组中的元素

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

2.3数组的属性 :length

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

2.4数组的遍历

// 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]); // 遍历的是二维数组中元素(一维数组)的元素
            }
        }

2.5 二维数组元素的默认值

1.二维数组元素的默认值是null

2.6 二维数组的内存分析

3、数组常见算法

        // 求数组元素的最大值
	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;
					}
				}
			}
		}
	}

冒泡算法:

        冒泡排序:
        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、Arrays工具类的使用

            

5、数组常见异常

第一种 : 空指针异常 - NullPointerException
第二种 :下角标越界 - IndexOutofBoundsException

代码:

    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、可变个数形参

格式:方法名(变量类型...变量名)
说明:

    1. 可变形参的个数可以是0个1个或者多个
    2. 如果一个方法中除了可变形参还有其它的参数,那么可变形参必须放在最后。
    3. 一个方法中只能有一个可变形参
    4. 可变参数方法的使用与方法参数部分使用数组是一致的
原文地址:https://www.cnblogs.com/wushaopei/p/12204097.html