Java基础之数组

定义:

dataType[]arrayName  // 首选的方法

dataType arrayName[] // 效果相同,但不是首选方法

dataType为Java中的任意数据类型,包括基本类型和组合类型,arrayName为数组名,必须是一个合法的标识符,[ ] 指明该变量是一个数组类型变量,arraySize为数组的初始长度,元素数超过初始长度,会以初始长度为单位递增存储空间。

初始化:

dataType[]arrayName = new dataType[arraySize] 

dataType arrayName[]= new dataType[arraySize] 

arraySize为数组的长度。

1:byte、short、int、long类型数组,数组元素默认初始化为0。

2:float、double类型数组,数组元素默认初始化为0.0。

3:char类型数组,数组元素默认初始化为  。

4:boolean类型数组,数组元素默认初始化为false。

5:String类型数组,数组元素默认初始化为null。

下标越界:  java.lang.ArrayIndexOutOfBoundsExceptionjava.lang.ArrayIndexOutOfBoundsException
 

代码:

public class main {
    public static void main(String[] args) {
        //byte
        System.out.println("<================byte================>");
        byte[] byteName = new byte[5];
        for (int i = 0; i < byteName.length; i++) {
            System.out.println("第"+i+"个byteName初始化值"+byteName[i]);
        }
        //赋值
        byte[] byName =new byte[]{ 5,6,7};//长度随数组元素增减而增减
        for (int i = 0; i < byName.length; i++) {
            System.out.println("第"+i+"个byName值"+byteName[i]);;
        }

        //short
        System.out.println("<================short================>");
        short[] shortName = new short[5];
        for (int i = 0; i < shortName.length; i++) {
            System.out.println("第"+i+"个shortName初始化值"+shortName[i]);
        }
        //赋值
         shortName =new short[]{ 5,6,7};
        for (int i = 0; i < shortName.length; i++) {
            System.out.println("第"+i+"个shortName值"+shortName[i]);;
        }

        //int
        System.out.println("<================int================>");
        //初始化
        int[] intName = new int[5];//元素不能超过定义数
        int intsName[] = new int[3];
        for (int i = 0; i < intName.length; i++) {
            System.out.println("第"+i+"个intName初始化值"+intName[i]);
        }
        for (int i = 0; i < intsName.length; i++) {
            System.out.println("第"+i+"个intsName初始化值"+intsName[i]);
        }
        //赋值
        intName = new int[]{ 1, 2, 3,4,5,7};
        //arrName.length 数组长度 (数组中有多少个元素) i数组下标
        for (int i = 0; i < intName.length; i++) {
            System.out.println("第"+i+"个intName值"+intName[i]);
        }

        //long
        System.out.println("<================long================>");
        long[] longtName = new long[10];
        for (int i = 0; i < longtName.length; i++) {
            System.out.println("第"+i+"个shortName初始化值"+longtName[i]);
        }
        //赋值
        longtName =new long[]{ 5,6,7,8,9};
        for (int i = 0; i < longtName.length; i++) {
            System.out.println("第"+i+"个longtName值"+longtName[i]);
        }

        //float
        System.out.println("<================float================>");
        float[] floatName = new float[4];
        for (int i = 0; i < floatName.length; i++) {
            System.out.println("第"+i+"个floatName初始化值"+floatName[i]);
        }
        //赋值
        floatName =new float[]{(float) 4.2,6,(float)7.5,8,9};
        for (int i = 0; i < floatName.length; i++) {
            System.out.println("第"+i+"个floatName值"+floatName[i]);
        }


        //double
        System.out.println("<================double================>");
        double[] doubleName = new double[2];
        for (int i = 0; i < doubleName.length; i++) {
            System.out.println("第"+i+"个doubleName初始化值"+doubleName[i]);
        }
        //赋值
        doubleName =new double[]{ 4.2,6.9999,7.5,8,9};
        for (int i = 0; i < doubleName.length; i++) {
            System.out.println("第"+i+"个doubleName值"+doubleName[i]);
        }

        //char
        System.out.println("<================char================>");
        char[] charName = new char[2];
        for (int i = 0; i < charName.length; i++) {
            System.out.println("第"+i+"个charName初始化值"+charName[i]);
        }
        //赋值
        charName =new char[]{ 'x','y','z'};
        for (int i = 0; i < charName.length; i++) {
            System.out.println("第"+i+"个charName值"+charName[i]);
        }

        //boolean
        System.out.println("<================boolean================>");
        boolean[] booleanName = new boolean[3];
        for (int i = 0; i < booleanName.length; i++) {
            System.out.println("第"+i+"个booleanName初始化值"+booleanName[i]);
        }
        //赋值
        booleanName =new boolean[]{ true,false,false};
        for (int i = 0; i < booleanName.length; i++) {
            System.out.println("第"+i+"个booleanName值"+booleanName[i]);
        }

        //String
        System.out.println("<================String================>");
        String[] StringName = new String[3];
        for (int i = 0; i < StringName.length; i++) {
            System.out.println("第"+i+"个StringName初始化值"+StringName[i]);
        }
        //赋值
        StringName =new String[]{ "郑州","洛阳","周口"};
        for (int i = 0; i < StringName.length; i++) {
            System.out.println("第"+i+"个StringName值"+StringName[i]);
        }
        //下标越界 java.lang.ArrayIndexOutOfBoundsExceptionjava.lang.ArrayIndexOutOfBoundsException
        System.out.println("start下标越界");
        for (int i = 0; i < 4; i++) {
            System.out.println("第"+i+"个StringName值"+StringName[i]);
        }
        System.out.println("end下标越界");
    }
}

 

你可能感兴趣的:(java)