当前位置:
文档之家› java 实验七 对象集合 实验报告
java 实验七 对象集合 实验报告
importjava.util.List;
classWorker {
privateintage;
privateStringname;
privatedoublesalary;
publicWorker (){}
publicWorker (String name,intage,doublesalary){
= name;
this.age = age;
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
list.add("Hello");
list.add("World");
list.add(1,"Learn");
list.add(1,"Java");
printList(list);
}
publicstaticvoidprintList(Listlist){
//1
for(Object o : list) {
List<Worker> list=newArrayList<Worker>();
Worker w1=newWorker("赵一",30,5600);
Worker w2=newWorker("钱二",21,3800);
Worker w3=newWorker("孙三",28,4200);
list.add(w1);
}
public void work(){
System.out.println(name + “ work”);
}
}
完成下面的要求
1)创建一个List,在List中增加三个工人,基本信息包括:姓名年龄工资。
答:增加三个工人赵一,钱二,孙三及其他们的年龄和工资,其代码如下:
packagezi;
importjava.util.ArrayList;
}
}
}
运行结果如下,不知道为啥会出现乱码。
2)删除一个记录的信息
答:其代码如下:
packagezi;
importjava.util.ArrayList;
importjava.util.List;
classWorker{
privateintage;
privateStringname;
privatedoublesalary;
Worker w2=newWorker("钱二",21,3800);
Worker w3=newWorker("孙三",28,4200);
list.add(w1);
list.add(w2);
list.add(w3);
}
}
2)在第二个记录之前插入一个工人信息
答:插入后代码如下:
packagezi;
importjava.util.ArrayList;
= name;
}
publicdoublegetSalary(){
returnsalary;
}
publicvoidsetSalary(doublesalary){
this.salary= salary;
}
publicvoidwork(){
System.out.println(name+" work");
2)三种代码与执行结果截图
1、用Iterator实现的代码如下:
packagezi;
importjava.util.*;
publicclassEx7_2{
publicstaticvoidmain(String args[]){
Listlist =newArrayList();
list.add("Hello");
2. (List)有如下代码:
import java.util.*;
public class TestList{
public static void main(String args[]){
List list = new ArrayList();
list.add(“Hello”);
list.add(“World”);
= name;
this.age= age;
this.salary= salary;
}
publicintgetAge() {
returnage;
}
publicvoidsetAge(intage) {
this.age= age;
}
publicString getName() {
returnname;
list.add(1, “Learn”);
list.add(1, “Java”);
printList(list);
}
public static void printList(List list){
//1
}
}
要求:
1) 把//1 处的代码补充完整,要求输出list 中所有元素的内容,请使用三种方式来实现(课本227页,Iterator、增强for循环,普通循环)。
System.out.println(o);
}
}
}
其运行结果截图如下:
3、用普通循环的代码如下:
packagezi;
importjava.util.*;
publicclassEx7_2{
publicstaticvoidmain(String args[]){
Listlist =newArrayList();
System.out.println(list.get(i));
}
}
}
其运行结果截图如下:
3) 如果要把实现类由ArrayList 换为LinkedList,应该改哪一句?怎么修改?ArrayList和LinkedList 使用上有什么区别?请书面回答
答:List list = new LinkedList();
Vector:以线性结构存储,查询速度较慢,线程安全。
3.(List)已知有一个Worker类如下:
public class Worker {
private int age;
private String name;
private double salary;
public Worker (){}
public Worker (String name, int age, double salary){
this.salary= salary;
}
publicvoidwork(){
System.out.println(name+" work");
importjava.util.List;
classWorker{
privateintage;
privateStringname;
privatedoublesalary;
publicWorker (){}
publicWorker (String name,intage,doublesalary){
= name;
System.out.println(name+" work");
}
}
publicclassEx7_3_1{
publicstaticvoidmain(String args[]){
List<Worker> list=newArrayList<Worker>();
Worker w1=newWorker("赵一",30,5600);
list.add("World");
list.add(1,"Learn");
list.add(1,"Java");
printList(list);
}
publicstaticvoidprintList(Listlist){
//1
Iteratoritor = list.iterator();
while(itor.hasNext()){
this.age= age;
}
publicString getName() {
returnname;
}
publicvoidsetName(String name) {
= name;
}
publicdoublegetSalary(){
returnsalary;
}
publicvoidsetSalary(doublesalary){
list.add(1, w2);
list.add(w2);
list.add(w3);
Worker w2=newWorker("孙三",28,4200);
list.remove(3);
for(inti = 0; i < list.size(); i++) {
System.out.println(list.get(i));
this.age= age;
this.salary= salary;
}
publicintgetAge() {
returnage;
}
publicvoidsetAge(intage) {
this.age= age;