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

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();

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值