java中对象数组_java对象数组怎么实现?对象数组是什么?

数组小伙伴们都接触过了,那对象数组是什么意思小伙伴们知道吗?它又该怎么实现呢?下面就一起看看吧。

对象数组概念及实现public class Student

// 成员变量

private String name;

private int age;

// 构造方法

public Student()

{

super();

}

public Student(String name, int age)

{

super();

this.name = name;

this.age = age;

}

// 成员方法

// getXxx()/setXxx()

public String getName()

{

return name;

}

public void setName(String name)

{

this.name = name;

}

public int getAge()

{

return age;

}

public void setAge(int age)

{

this.age = age;

}

@Override

public String toString()

{

return "Student [name=" + name + ", age=" + age + "]";

}

}/**

把5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。

*         学生:Student

*         成员变量:name,age

*         构造方法:无参,带参

*         成员方法:getXxx()/setXxx()

* 分析:

*         A:创建学生类。

*         B:创建学生数组(对象数组)。

*         C:创建5个学生对象,并赋值。

*         D:把C步骤的元素,放到数组中。

*         E:遍历学生数组。

*  */

public class Practice

{

public static void main(String[] args)

{

// 创建学生数组(对象数组)。

Student[] students = new Student[5];

// for (int x = 0; x 

// {

//         System.out.println(students[x]);

// }

//     System.out.println("---------------------");

// 创建5个学生对象,并赋值。

Student s1 = new Student("小明", 27);

Student s2 = new Student("小红", 30);

Student s3 = new Student("小强", 30);

Student s4 = new Student("旺财", 12);

Student s5 = new Student("张三", 35);

// 将对象放到数组中。

students[0] = s1;

students[1] = s2;

students[2] = s3;

students[3] = s4;

students[4] = s5;

// 遍历

for (int x = 0; x 

{

//System.out.println(students[x]);

Student s = students[x];

System.out.println(s.getName() + "---" + s.getAge());

}

}

}

对象数组内存图解

2011e935f7aeb1669b2a77281df640d3.png

以上就是今天的所有内容,对于数组小伙伴们如果还有疑问,并且想了解更多java项目中常见问题,就快一直关注我们网站吧。

推荐阅读:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值