Java 基础一些代码练习笔记(Array数组)

来源:互联网 发布:编程教程视频 编辑:程序博客网 时间:2024/06/06 07:17
public class ArrayTest{public static void main(String[] args){int[] a = new int[4];a[0] = 1;a[1] = 2;a[2] = 3;a[3] = 4;System.out.println(a[3]);System.out.println("-------------------");int[] b = {1,2,3,4};System.out.println(b[2]);System.out.println("-------------------");int[] c = new int[]{1,2,3,4};System.out.println(c[3]);System.out.println("-------------------");int[] d = new int[100];for(int i = 0; i < d.length; i++){d[i] = i + 1;}for (int i = 0;i < d.length; i++ ){System.out.println(d[i]);}}}


public class ArrayTest2{public static void main(String[] args){Person[] p = new Person[3];p[0] = new Person(10);p[1] = new Person(20);p[2] = new Person(30);for (int i = 0;i < p.length; i++ ){System.out.println(p[i].age);}}}class Person{int age;public Person(int age){this.age = age;}}



public class ArrayTest4{public static void main(String[] args){int [][] a = new int[2][3];int m = 0;for (int i = 0; i < 2; i++ )//遍历数组行{for (int j = 0; j < 3; j++ )//列{m++;a[i][j] = 2 * m;}}for (int i = 0; i < 2; i++ )//遍历数组行{for (int j = 0; j < 3; j++ )//列{System.out.println( a[i][j]);}}}}


class Person{public int age;public double height;public void info(){System.out.println("我的年龄是: "+ age + ",我的身高是: " + height);}}public class ArrayTest6{public static void main(String[] args){Person[] student = new Person[2]; //定义数组Person zhang = new Person();zhang.age = 26;zhang.height = 158;Person lee = new Person();lee.age = 21;lee.height = 184;student[0] = zhang;student[1] = lee;//lee和student[1]指的是同一个Person实例lee.info();student[1].info();}}


原创粉丝点击