数组元素与普通变量


/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> 
 * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */

class Cat
{
	double weight;
	int age;
	public Cat(double weight , int age)
	{
		this.weight = weight;
		this.age = age;
	}
}
public class ArrayTest
{
	public static void main(String[] args) 
	{
		//定义,并动态初始化一个int[]数组
		int[] pos = new int[5];
		//采用循环为每个数组元素赋值
		for (int i = 0; i < pos.length ; i++ )
		{
			pos[i] = (i + 1) * 2;
		}
		//对于pos数组的元素来说,用起来完全等同于普通变量
		//下面即可将数组元素的值赋给int变量,也可将int变量的值赋给数组元素
		int a = pos[1];
		int b = 20;
		pos[2] = b;
		//定义,并动态初始化一个Cat[]数组
		Cat[] cats = new Cat[2];
		cats[0] = new Cat(3.34, 2);
		//将cats数组的第1个元素的值赋给c1。
		Cat c1 = cats[0];
		Cat c2 = new Cat(4.3, 3);
		//将c2的值赋给cats数组的第2个元素
		cats[1] = c2;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值