java数组

Posted qianyyue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java数组相关的知识,希望对你有一定的参考价值。

1、一维数组

public class Testarray {
    public static void main(String[] args) {
        int t = 1;
        //数组的定义(二者缺一不可):声明(数据类型、变量名[])、分配空间
        int arr[] = new int[4];
        int arr2[];
        arr2 = new int[3];
        int[] arr3 = new int[5];
        int[] arr4;
        arr4 = new int[6];
        String strs[] = new String[3];
        
        //数组元素访问时,数组的下标从零开始
        //为数组元素赋值
        arr[0] = 2;
        arr[1] = 5;
        arr[2] = 6;
        arr[3] = 1;
        
        strs[0] = "aaa";
        strs[1] = "eee";
        strs[2] = "www";
        
        System.out.println(arr[0]);
        System.out.println(strs[2]);
        
        //数组元素的长度,一旦分配空间后,数组长度不可变
        System.out.println(arr.length);
        
        //数组元素的遍历
        System.out.println(arr[0]);
        for(int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        
        //数组默认值(或者叫初始化值,在声明和分配空间后,不赋值)
        //对于byte、short、int、long而言,默认值为0
        long[] a1 = new long[5];
        for(int i = 0; i < a1.length; i++) {
            System.out.println(a1[i]);
        }
        //对于float、double而言,默认值是0.0
        double[] a2 = new double[6];
        for(int i = 0; i < a2.length; i++) {
            System.out.println(a2[i]);
        }
        //对于char而言,默认值是空格
        char[] a3 = new char[5];
        for(int i = 0; i < a3.length; i++) {
            System.out.println(a3[i]);
        }
        //对于boolean而言,默认值是false
        boolean[] a4 = new boolean[3];
        for(int i = 0; i < a4.length; i++) {
            System.out.println(a4[i]);
        }
        //引用数据类型,默认值是null
        String[] a5 = new String[6];
        for(int i = 0; i < a5.length; i++) {
            System.out.println(a5[i]);
        }
        
        //定义数组的其他方式
        int arr6[] = new int[] {3,6,7,2,1};
        int arr7[] = {3,6,7,2,1};
        
        String arr8[] = new String[] {"a","gg","ww"};
        
        arr6[0] = 50;
        System.out.println(arr6.length);
        System.out.println(arr6[0]);
        for(int i = 0; i < arr6.length; i++) {
            System.out.println(arr6[i]);
        }
        //练习
        String[] arr9 = new String[]{"Mike","jimmy","tommy","jerry"};
        for(int i = 0; i < arr9.length; i++) {
            System.out.println(arr9[i]);
        }
        
        int a[] = new int[100];
        for(int i = 0; i < a.length; i++) {
            a[i] = (i + 1) * 2;
            System.out.println(a[i]);
        }
    }
}

2、二维数组

public class Testarray2 {
    public static void main(String[] args) {
        //定义
        int arr[][] = new int[3][2];
        int[] arr2[] = new int[5][2];
        int[][] arr3 = new int[3][5];
        //赋值
        arr[0][0] = 2;
        arr[0][1] = 5;
        arr[1][0] = 2;
        arr[1][1] = 6;
        arr[2][0] = 1;
        arr[2][1] = 5;
        //数组元素长度
        System.out.println(arr.length);//行数
        System.out.println(arr[0].length);//第0行的列数
        //遍历
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        
        //定义数组其他方式
        int arr4[][] = new int[3][];
        arr4[0] = new int[4];
        arr4[1] = new int[3];
        arr4[2] = new int[5];
        
        System.out.println(arr4.length);
        System.out.println(arr4[2].length);
        for(int i = 0; i < arr4.length; i++) {
            for(int j = 0; j < arr4[i].length; j++) {
                System.out.print(arr4[i][j] + " ");
            }
            System.out.println();
        }
        
        
        int[][] arr5 = new int[][] {{3,7},{2,5,8},{2,6,9,5}};
        int[][] arr6 = {{3,7},{2,5,8},{2,6,9,5}};
        for(int i = 0; i < arr6.length; i++) {
            for(int j = 0; j < arr6[i].length; j++) {
                System.out.print(arr6[i][j] + " ");
            }
            System.out.println();
        }
    }
}

3、算法

public class Testarray3 {
    public static void main(String[] args) {
        int[] arr = new int[] {12,43,9,0,-65,-99,100,9};
        //求最大值
        int max = arr[0];
        for(int i = 1; i < arr.length; i++) {
            if(max < arr[i]) {
                max = arr[i];
            }
        }
        System.out.println(max);
        
        //求最小值
        int min = arr[0];
        for(int i = 1; i < arr.length; i++) {
            if(min > arr[i]) {
                min = arr[i];
            }
        }
        System.out.println(min);
        
        //求和
        int sum = 0;
        for(int i = 0; i < arr.length; i++) {
            sum = sum + arr[i];
        }
        System.out.println(sum);
        
        //交换两个数的值
        int x = 5;
        int y = 8;
        int a;
        a = x;
        x = y;
        y = a;
        System.out.println(x);
        System.out.println(y);    
    }
}

4、冒泡排序

import java.util.Arrays;

public class Testarray4 {
    public static void main(String[] args) {
        //冒泡排序
        int[] arr = new int[] {6,5,4,3,2};
        
        for(int i = 0; i < arr.length - 1; i++) {
            for(int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[ j + 1];
                    arr[ j + 1] = temp;
                }
            }
        }
        for(int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        
//利用JDK提供的工具也可以排序
int[] arr1 = new int[] {6,5,4,3,2}; Arrays.sort(arr1); for(int i = 0; i < arr1.length; i++) { System.out.println(arr1[i]); } } }

 

以上是关于java数组的主要内容,如果未能解决你的问题,请参考以下文章

Java经典安全数组实现的代码

java.io.ByteArrayInputStream

web代码片段

错误代码:错误域 = NSCocoaErrorDomain 代码 = 3840“JSON 文本没有以数组或对象和允许未设置片段的选项开头。”

javascript常用代码片段

常用Java程序片段