java 实验七 对象集合 实验报告
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
importjava.util.List;
classWorker {
privateintage;
privateStringname;
privatedoublesalary;
publicWorker (){}
publicWorker (String name,intage,doublesalary){
this.name = name;
this.age = age;
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
}
}
}
运行结果如下,不知道为啥会出现乱码。
2)删除一个记录的信息
答:其代码如下:
packagezi;
importjava.util.ArrayList;
importjava.util.List;
classWorker{
privateintage;
privateStringname;
privatedoublesalary;
}
public void work(){
System.out.println(name + “ work”);
}
}
完成下面的要求
1)创建一个List,在List中增加三个工人,基本信息包括:姓名年龄工资。
答:增加三个工人赵一,钱二,孙三及其他们的年龄和工资,其代码如下:
packagezi;
importjava.util.ArrayList;
ArrayList :查询数据速度较快,插入和删除比较慢,线程非安全。
LinkedList: 查询数据速度较慢,插入和删除比较快,线程安全。
4) 如果要把实现类由ArrayList 换为Vector,应该改哪一句?怎修改?ArrayList 和Vector 使用上有什么区别?请书面回答。
答:List list = new Vector();
System.out.println(name+" work");
}
}
publicclassEx7_3_1{
publicstaticvoidmain(String args[]){
List<Worker> list=newArrayList<Worker>();
Worker w1=newWorker("赵一",30,5600);
this.salary= salary;
}
publicvoidwork(){
System.out.println(name+" work");
}
@Override
publicString toString() {
return"Worker [age="+age+", name="+name+", salary="+salary+"]";
}
}
publicclassEx7_3_3{
publicstaticvoidmain(String args[]){
实验七对象集合的组织
实验目标:
1.了解Collection、List、Set、Map等常用数据结构的层次结构
2.掌握LinkList、Vector、ArrayList、HashTable、HashMap等类的用法
3.掌握Enumeration(枚举)及Iterator(迭代器)的遍历用法
4.了解自学HashSet、TreeSet这两个Set接口下的重要实现类
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(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){
list.add("Hello");
list.add("World");
list.add(1,"Learn");
list.add(1,"Java");
printList(list);
}
publicstaticvoidprintList(Listlist){
//1
for(inti = 0; i< list.size();i++){
this.agቤተ መጻሕፍቲ ባይዱ= age;
this.salary= salary;
}
publicintgetAge() {
returnage;
}
publicvoidsetAge(intage) {
this.age= age;
}
publicString getName() {
returnname;
}
publicvoidsetName(String name) {
list.add("Hello");
list.add("World");
list.add(1,"Learn");
list.add(1,"Java");
printList(list);
}
publicstaticvoidprintList(Listlist){
//1
for(Object o : list) {
list.add("World");
list.add(1,"Learn");
list.add(1,"Java");
printList(list);
}
publicstaticvoidprintList(Listlist){
//1
Iteratoritor = list.iterator();
while(itor.hasNext()){
2)三种代码与执行结果截图
1、用Iterator实现的代码如下:
packagezi;
importjava.util.*;
publicclassEx7_2{
publicstaticvoidmain(String args[]){
Listlist =newArrayList();
list.add("Hello");
System.out.println(o);
}
}
}
其运行结果截图如下:
3、用普通循环的代码如下:
packagezi;
importjava.util.*;
publicclassEx7_2{
publicstaticvoidmain(String args[]){
Listlist =newArrayList();
this.age= age;
}
publicString getName() {
returnname;
}
publicvoidsetName(String name) {
this.name= name;
}
publicdoublegetSalary(){
returnsalary;
}
publicvoidsetSalary(doublesalary){
this.name= name;
this.age= age;
this.salary= salary;
}
publicintgetAge() {
returnage;
}
publicvoidsetAge(intage) {
this.age= age;
}
publicString getName() {
returnname;
publicWorker (){}
publicWorker (String name,intage,doublesalary){
this.name= name;
this.age= age;
this.salary= salary;
}
publicintgetAge() {
returnage;
}
publicvoidsetAge(intage) {
5.了解自学比较器接口
实验任务:
1.填空
List接口的特点是元素__有__(有|无)顺序,___可以___(可以|不可以)重复;
Set接口的特点是元素__无___(有|无)顺序,__不可以____(可以|不可以)重复;
Map接口的特点是元素是键、值对象,其中__键__可以重复,_值对象__不可以重复
List<Worker> list=newArrayList<Worker>();
Worker w1=newWorker("赵一",30,5600);
Worker w2=newWorker("钱二",21,3800);
Worker w3=newWorker("孙三",28,4200);
list.add(w1);
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary(){
return salary;
}
public void setSalary(double salary){
this.salary = salary;
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));
System.out.println(itor.next());
}
}
}
其运行结果截图如下:
2、用增强for循环的代码如下:
packagezi;
importjava.util.*;
publicclassEx7_2{
publicstaticvoidmain(String args[]){
Listlist =newArrayList();
2. (List)有如下代码:
import java.util.*;
public class TestList{
public static void main(String args[]){
List list = new ArrayList();
list.add(“Hello”);
list.add(“World”);
}
publicvoidsetName(String name) {
this.name= name;
}
publicdoublegetSalary(){
returnsalary;
}
publicvoidsetSalary(doublesalary){
this.salary= salary;
}
publicvoidwork(){
importjava.util.List;
classWorker{
privateintage;
privateStringname;
privatedoublesalary;
publicWorker (){}
publicWorker (String name,intage,doublesalary){
this.name= name;
this.name= name;
}
publicdoublegetSalary(){
returnsalary;
}
publicvoidsetSalary(doublesalary){
this.salary= salary;
}
publicvoidwork(){
System.out.println(name+" work");
Worker w2=newWorker("钱二",21,3800);
Worker w3=newWorker("孙三",28,4200);
list.add(w1);
list.add(w2);
list.add(w3);
}
}
2)在第二个记录之前插入一个工人信息
答:插入后代码如下:
packagezi;
importjava.util.ArrayList;
classWorker {
privateintage;
privateStringname;
privatedoublesalary;
publicWorker (){}
publicWorker (String name,intage,doublesalary){
this.name = name;
this.age = age;
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
}
}
}
运行结果如下,不知道为啥会出现乱码。
2)删除一个记录的信息
答:其代码如下:
packagezi;
importjava.util.ArrayList;
importjava.util.List;
classWorker{
privateintage;
privateStringname;
privatedoublesalary;
}
public void work(){
System.out.println(name + “ work”);
}
}
完成下面的要求
1)创建一个List,在List中增加三个工人,基本信息包括:姓名年龄工资。
答:增加三个工人赵一,钱二,孙三及其他们的年龄和工资,其代码如下:
packagezi;
importjava.util.ArrayList;
ArrayList :查询数据速度较快,插入和删除比较慢,线程非安全。
LinkedList: 查询数据速度较慢,插入和删除比较快,线程安全。
4) 如果要把实现类由ArrayList 换为Vector,应该改哪一句?怎修改?ArrayList 和Vector 使用上有什么区别?请书面回答。
答:List list = new Vector();
System.out.println(name+" work");
}
}
publicclassEx7_3_1{
publicstaticvoidmain(String args[]){
List<Worker> list=newArrayList<Worker>();
Worker w1=newWorker("赵一",30,5600);
this.salary= salary;
}
publicvoidwork(){
System.out.println(name+" work");
}
@Override
publicString toString() {
return"Worker [age="+age+", name="+name+", salary="+salary+"]";
}
}
publicclassEx7_3_3{
publicstaticvoidmain(String args[]){
实验七对象集合的组织
实验目标:
1.了解Collection、List、Set、Map等常用数据结构的层次结构
2.掌握LinkList、Vector、ArrayList、HashTable、HashMap等类的用法
3.掌握Enumeration(枚举)及Iterator(迭代器)的遍历用法
4.了解自学HashSet、TreeSet这两个Set接口下的重要实现类
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(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){
list.add("Hello");
list.add("World");
list.add(1,"Learn");
list.add(1,"Java");
printList(list);
}
publicstaticvoidprintList(Listlist){
//1
for(inti = 0; i< list.size();i++){
this.agቤተ መጻሕፍቲ ባይዱ= age;
this.salary= salary;
}
publicintgetAge() {
returnage;
}
publicvoidsetAge(intage) {
this.age= age;
}
publicString getName() {
returnname;
}
publicvoidsetName(String name) {
list.add("Hello");
list.add("World");
list.add(1,"Learn");
list.add(1,"Java");
printList(list);
}
publicstaticvoidprintList(Listlist){
//1
for(Object o : list) {
list.add("World");
list.add(1,"Learn");
list.add(1,"Java");
printList(list);
}
publicstaticvoidprintList(Listlist){
//1
Iteratoritor = list.iterator();
while(itor.hasNext()){
2)三种代码与执行结果截图
1、用Iterator实现的代码如下:
packagezi;
importjava.util.*;
publicclassEx7_2{
publicstaticvoidmain(String args[]){
Listlist =newArrayList();
list.add("Hello");
System.out.println(o);
}
}
}
其运行结果截图如下:
3、用普通循环的代码如下:
packagezi;
importjava.util.*;
publicclassEx7_2{
publicstaticvoidmain(String args[]){
Listlist =newArrayList();
this.age= age;
}
publicString getName() {
returnname;
}
publicvoidsetName(String name) {
this.name= name;
}
publicdoublegetSalary(){
returnsalary;
}
publicvoidsetSalary(doublesalary){
this.name= name;
this.age= age;
this.salary= salary;
}
publicintgetAge() {
returnage;
}
publicvoidsetAge(intage) {
this.age= age;
}
publicString getName() {
returnname;
publicWorker (){}
publicWorker (String name,intage,doublesalary){
this.name= name;
this.age= age;
this.salary= salary;
}
publicintgetAge() {
returnage;
}
publicvoidsetAge(intage) {
5.了解自学比较器接口
实验任务:
1.填空
List接口的特点是元素__有__(有|无)顺序,___可以___(可以|不可以)重复;
Set接口的特点是元素__无___(有|无)顺序,__不可以____(可以|不可以)重复;
Map接口的特点是元素是键、值对象,其中__键__可以重复,_值对象__不可以重复
List<Worker> list=newArrayList<Worker>();
Worker w1=newWorker("赵一",30,5600);
Worker w2=newWorker("钱二",21,3800);
Worker w3=newWorker("孙三",28,4200);
list.add(w1);
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary(){
return salary;
}
public void setSalary(double salary){
this.salary = salary;
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));
System.out.println(itor.next());
}
}
}
其运行结果截图如下:
2、用增强for循环的代码如下:
packagezi;
importjava.util.*;
publicclassEx7_2{
publicstaticvoidmain(String args[]){
Listlist =newArrayList();
2. (List)有如下代码:
import java.util.*;
public class TestList{
public static void main(String args[]){
List list = new ArrayList();
list.add(“Hello”);
list.add(“World”);
}
publicvoidsetName(String name) {
this.name= name;
}
publicdoublegetSalary(){
returnsalary;
}
publicvoidsetSalary(doublesalary){
this.salary= salary;
}
publicvoidwork(){
importjava.util.List;
classWorker{
privateintage;
privateStringname;
privatedoublesalary;
publicWorker (){}
publicWorker (String name,intage,doublesalary){
this.name= name;
this.name= name;
}
publicdoublegetSalary(){
returnsalary;
}
publicvoidsetSalary(doublesalary){
this.salary= salary;
}
publicvoidwork(){
System.out.println(name+" work");
Worker w2=newWorker("钱二",21,3800);
Worker w3=newWorker("孙三",28,4200);
list.add(w1);
list.add(w2);
list.add(w3);
}
}
2)在第二个记录之前插入一个工人信息
答:插入后代码如下:
packagezi;
importjava.util.ArrayList;