本篇博文最后修改时间:2016年2月29日,18:48。
本篇介绍数组中元素的表示方法。
系统版本:Windows7 家庭普通版 32位操作系统。
三、版权声明
博主:思跡
声明:喝水不忘挖井人,转载请注明出处。
原文地址:http://blog.youkuaiyun.com/omoiato
联系方式:315878825@qq.com
Java零基础入门交流群:541462902
四、数组中元素的表示方法
1、为数组的元素赋值
范例:
public class ArrayDemo02
{
public static void main(String [] args)
{
int score[] = null; //声明数组
score = new int[3]; //为数组开辟空间,大小为3
for(int x=0;x<3; x++) //为名一个元素赋值
{
score[x] = x * 2 + 1; //每一个值都是奇数
}
for(int x=0; x<3; x++ ) //使用循环依次输出数组的全部内容
{
System.out.println("score["+x+"] =" + score[x]);
}
}
}
范例程序运行结果:
以上程序的内存操作流程如下图:
2、数组长度的取得
格式:
数组名称.length ->返回一个int型数据
范例:
public class ArrayDemo03
{
public static void main(String [] args)
{
int score[] = new int[3]; //声明并实例化数组
System.out.println("数组长度为" + score.length); //求出数组长度
}
}
程序运行结果: