美文网首页
集合框架(对象数组的概述和使用)

集合框架(对象数组的概述和使用)

作者: 养码哥 | 来源:发表于2018-04-01 20:22 被阅读0次

核心代码:

package cn.ithelei;

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 + "]";
     }
}

ObjectArrayDemo

 package cn.ithelei;

/*
 * 我有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息。
 *       学生:Student
 *       成员变量:name,age
 *       构造方法:无参,带参
 *       成员方法:getXxx()/setXxx()
 *       存储学生的数组?自己想想应该是什么样子的?
 * 分析:
 *      A:创建学生类。
 *      B:创建学生数组(对象数组)。
 *      C:创建5个学生对象,并赋值。
 *      D:把C步骤的元素,放到数组中。
 *      E:遍历学生数组。
 */
public class ObjectArrayDemo {
    public static void main(String[] args) {

    // 创建学生数组(对象数组)。
    Student[] students = new Student[5];
    // for (int x = 0; x < students.length; x++) {
    // System.out.println(students[x]);
    // }
    // System.out.println("---------------------");

    // 创建5个学生对象,并赋值。
    Student s1 = new Student("林青霞", 27);
    Student s2 = new Student("梅超风", 30);
    Student s3 = new Student("小哥", 18);
    Student s4 = new Student("赵雅芝", 60);
    Student s5 = new Student("王力宏", 35);

    // 把C步骤的元素,放到数组中。
    students[0] = s1;
    students[1] = s2;
    students[2] = s3;
    students[3] = s4;
    students[4] = s5;

    // 看到很相似,就想循环改
    // for (int x = 0; x < students.length; x++) {
    // students[x] = s + "" + (x + 1);
    // }
    // 这个是有问题的

    // 遍历
    for (int x = 0; x < students.length; x++) {
        // System.out.println(students[x]);

        Student s = students[x];
        System.out.println(s.getName() + "---" + s.getAge());
        }
    }

}
对象数组的内存图解

相关文章

  • Collection,List,Vector

    day15笔记【Collection,List,Vector】 1_集合框架(对象数组的概述和使用) A:案例演示...

  • 迭代器总结

    一.集合框架 1.集合框架(对象数组的概述和使用) a.案例演示 * 需求:我有5个学生,请把这个5个学生的信息存...

  • 集合框架(对象数组的概述和使用)

    核心代码: ObjectArrayDemo 邮箱:ithelei@sina.cn 技术讨论群:687856230 ...

  • 集合框架-对象数组的概述和使用

    例: 有五个学生,要把这五个学生的信息储存到数组中,并遍历数组,获取得到每一个学生的信息学生:Student成员变...

  • Topic15:Collection集合

    15.01 集合框架(对象数组的概述和使用) 打印一个对象的引用,默认调用的就是它的toString方法 toSt...

  • Java基础笔记总结(9)-集合(1)集合与数组转换 迭代器 L

    集合框架(对象数组的概述和使用) 结合类能存储任意对象,长度是可以改变的,随着元素的增加而增加,元素的减少而减少 ...

  • 集合框架(1)

    01_集合框架(对象数组的概述和使用) A:案例演示需求:我有5个学生,请把这个5个学生的信息存储到数组中,并遍历...

  • Java基础笔记15

    15.01_集合框架(对象数组的概述和使用) 案例演示需求:我有5个学生,请把这个5个学生的信息存储到数组中,并遍...

  • 10 Java集合

    集合框架体系概述 为什么出现集合类?方便多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方法. 数组...

  • JavaSE集合类

    JavaSE集合类 概述 Java中集合类概述Java中数组与集合的比较Java中集合框架层次结构 Collect...

网友评论

      本文标题:集合框架(对象数组的概述和使用)

      本文链接:https://www.haomeiwen.com/subject/gvmacftx.html