使用Java定义数组

目录

1.数组的创建

1.1语法

 1.2代码举例

1.3数组的使用

2.数组在方法的使用

 2.1基本用法

2.2引用类型

3. 数组作为方法的返回值

3.1传址

 3.2返回一个新的数组

 4.二维数组

4.1基本语法

 4.2遍历二维数组


1.数组的创建

1.1语法

// 动态初始化
数据类型 [] 数组名称 = new 数据类型 [] { 初始化数据 };
// 静态初始化
数据类型 [] 数组名称 = { 初始化数据 };

 1.2代码举例

public class TestArray {
    public static void main(String[] args) {
        int [] array = {1,2,3,4,5};
        int [] array2 = new int[3];
        int [] array3 = new int[]{1,2,3};
       
    }

}

1.3数组的使用

求数组长度

public class TestArray {
    public static void main(String[] args) {
        int [] array = {1,2,3,4,5};

        System.out.println(array.length);//求数组的长度
        System.out.println(array[2]);//打印,下标从零开始

        array[2] = 8;
        System.out.println(array[2]);//给下标为3的数重新赋值
       
    }

}

遍历数组

1.for循环,遍历数组
2.for-each 是for循环的另一种使用方式, 能够更方便的完成对数组的遍历,可以避免循环条件和更新语句写错
3.借助Java的操作数组的工具类 Arrays.toString  :将参数的数组  以字符串的形式进行输出
public class TestArray {
    public static void main(String[] args) {
        int [] array = {1,2,3,4,5};

          int[] array = {1,2,3,4,5};

        //1、for循环
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");//遍历数组,即打印数组中的数
        }
        System.out.println();//换行

        //2、for -each 
        for (int x: array ) {
            System.out.print(x+" ");
        }
        System.out.println();

        //3、字符串形式打印
        System.out.println(Arrays.toString(array));
       
    }

}

2.数组在方法的使用

 2.1基本用法

使用方法来遍历数组

int[] a 是函数的形参 , int[] array  是函数实参 .
如果需要获取到数组长度 , 同样可以使用 a.length
public class TestArray {

    private static void printf(int[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int [] array = {1,2,3,4,5};

        printf(array);
       
    }

}

2.2引用类型

将函数的实参传到方法中的形参,如果形参中某个元素改变,实参也会跟着改变

如果在方法中new一个新的数组来接收,则地址会改变,如果我们要打印出来的话,是是不会影响到元数组的,即实参不会被影响

public class TestArray {

     public static void func1(int[] array) {
        array = new int[]{3,4,5,2,8};//定义了一个新的数组,地址不一样,
                                      //所以改变不了原本实参的数组

    }
    public static void func2(int[] array) {
        array[0]=45;//使用的地址和原本是一样的,所以可以改变实参的数
    }

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};
        System.out.println("原数组:"+Arrays.toString(array));

        func1(array);
        System.out.println("定义新数组:"+Arrays.toString(array));
        
        func2(array);
        System.out.println("传形参数组:"+Arrays.toString(array));
    }

}

 

3. 数组作为方法的返回值

3.1传址

写一个数组,将数组的每一个元素加2;因为使用的是传址,在方法中加2时,原来的数组也会改变,这样就破坏了原来的数组。

public class TestArray {
    public static void add(int[] array) {
    //每个数组元素加2
        for (int i =0; i < array.length; i++) {
            array[i] = array[i] + 2;
        }
    }
   //遍历数组
    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
    }
    //主函数
    public static void main(String[] args) {
        int[] array = {1,2,3,4};
        add(array);
        printArray(array);
    }

}

 

 3.2返回一个新的数组

 new一个新的数组,是引用了原来数组的元素,不是引用原来数组的地址,所以返回的时候知识将这个数组的首元素返回给函数的调用者,没有拷贝数组的内容,从而更高效。

public class TestArray {
    //new新的数组,并且返回数组
    public static int[] form(int[] array) {
        int[] arr = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            arr[i] = array[i] + 2;
        }
        return arr;
    }
    //遍历新的数组
    public static void print(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+ " ");
        }
    }
    //主函数
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] put = form(array);
        print(put);
    }

}

 

 4.二维数组

二维数组本质上也是一个一维数组,只不过每个元素又是一个一维数组

4.1基本语法

数据类型 [ ][ ] 数组名称 = new 数据类型 [ 行数 ][ 列数 ] { 初始化数据 };

 4.2遍历二维数组

public class TestArray {

     public static int[][] Array(int[][] array1) {
        int[][] ret = new int[3][3];
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                ret[i][j] = array1[i][j];
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        int[][] array1 = {
   
   {1, 2, 3}, {2, 3, 4}, {3, 4, 5}};
        int[][] array2 = Array(array1);
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array2[i][j]+" ");
            }
            System.out.println();
        }
    }
}

  

猜你喜欢

转载自blog.csdn.net/m0_60494863/article/details/121701961