码迷,mamicode.com
首页 > 编程语言 > 详细

三、java数组

时间:2019-05-11 23:33:19      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:span   练习   长度   变量   main   new   style   rgs   二维数组   

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数组

标签:span   练习   长度   变量   main   new   style   rgs   二维数组   

原文地址:https://www.cnblogs.com/qianyyue/p/10850397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!