黑马程序员技术交流社区

标题: 【郑州校区】JavaEE实体基础班教程笔记第9天第1章 对象数组 [打印本页]

作者: 我是楠楠    时间: 2018-2-27 10:31
标题: 【郑州校区】JavaEE实体基础班教程笔记第9天第1章 对象数组
09天 java集合
今日内容介绍
第1章 对象数组

1.1 对象数组概述
A:基本类型的数组:存储的元素为基本类型
int[] arr={1,2,3,4}
B:对象数组:存储的元素为引用类型
  Student[] stus=new Student[3];
  
Student代表一个自定义类
Stus数组中stus[0],stus[1],stus[2]的元素数据类型为Student,
  都可以指向一个Student对象
1.2 对象数组案例:
创建一个学生数组,存储三个学生对象并遍历
1.2.1 案例代码一:
[AppleScript] 纯文本查看 复制代码
  package com.itheima;
/*
* 自动生成构造方法:
* 代码区域右键 -- Source -- Generate Constructors from Superclass...        无参构造方法
* 代码区域右键 -- Source -- Generate Constructor using Fields...        带参构造方法
* 自动生成getXxx()/setXxx():
* 代码区域右键 -- Source -- Generate Getters and Setters...
*/
public class Student {
private String name;
private int age;
public Student() {
}

public Student(String name, int age) {
this.name = name;
this.age = age;
}

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;
}
}
[AppleScript] 纯文本查看 复制代码
package com.itheima;
/*
* 创建一个学生数组,存储三个学生对象并遍历
*
* 分析:
* A:定义学生类
* B:创建学生数组
* C:创建学生对象
* D:把学生对象作为元素赋值给学生数组
* E:遍历学生数组
*/
public class StudentDemo {
public static void main(String[] args) {
//创建学生数组
Student[] students = new Student[3];
//创建学生对象
Student s1 = new Student("曹操",40);
Student s2 = new Student("刘备",35);
Student s3 = new Student("孙权",30);
//把学生对象作为元素赋值给学生数组
students[0] = s1;
students[1] = s2;
students[2] = s3;
//遍历学生数组
for(int x=0; x<students.length; x++) {
Student s = students[x];
//System.out.println(s);
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
1.3 对象数组的内存图
更多
第一天
第1章 Java概述
第2章 环境配置
第3章 语法格式
第二天
第1章 Eclipse开发工具
第2章 运算符
第3章 键盘录入
第三天
第1章 选择流程控制语句
第2章 循环流程控制语句
第3章 控制循环语句
第四天

传智播客·黑马程序员郑州校区地址
河南省郑州市 高新区长椿路11号大学科技园(西区)东门8号楼三层
联系电话 0371-56061160/61/62
来校路线  地铁一号线梧桐街站A口出







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2