千锋学习的第23天
二 (List) 有如下代码
import java.util.*;
public class TestList{
public static void main(String[] args ){
List list=new ArrayList();
list.add(“Hello”);
list.add(“Hello”);
list.add(1,“Learm”);
list.add(1,“java”);
printList(list);
}
public static void printList(List list){
//1
}
}
要求
- 把//1处的代码补充完整,要求输出list中所有的元素内容
System.out.println(list);
for(int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
- 写出程序的执行结果
[Helle, java, Learm, World]
Helle
java
Learm
World
3、如果要把实现类有ArrayList换成LinkedList,应该改在哪里?ArrayList和LinkedList使用上有什么区别,实现上有什么区别。
在这修改: List list=new LinkedList();
ArrayList : 是数组形式的连续的数据,优点查询快,增删慢
LinkedList:是链表结构的数据,数据不连续,依赖naxt和prev关联数据,增删快,查询慢
4、如果要把实现类有ArrayList换成Vector。应该改在哪里?ArrayList和Vector使用上有什么区别?实现上有什么区别?
在这修改: List list=new Vector();
使用上近似
Arraylist :运行效率快,线程不安全;
Vector :运行效率慢,线程安全;
三(list)写出下面程序的运行结果
import java.util.*
public class TestList{
public static void main(String args[]){
List list=new ArrayList();
list.add(“Hello”);
list.add(“World”);
list.add(“Hello”);
list.add(“Learm”);
list.add(“Hello”);
list.remove(0);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
程序结果
World
Hello
Learm
Hello
五(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.name=name;
this.age=age;
this.salary=salary;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
public void setSalary(double salary){
this.salary=salary;
}
public double getSalary(){
return salary;
}
public void work(){
System.out.println(name+”work”);
}
}
完成下面的要求
1创建一个list,在list中增加三个公然基本条件如下
姓名 年龄 工资
Zhang3 18 3000
Li4 25 3500
Wang5 22 3200
2,在li4之前插入一个工人,信息:姓名zhao6,年龄24,工资3300
3,删除wang5的信息
4,循环打印所有工人的信息
5,利用for-each遍历,对list中所有的工人调用work方法。
6,为worker类添加equals方法
import java.util.ArrayList;
import java.util.List;
public class TestWorker {
public static void main(String[] args) {
List list=new ArrayList();
Worker w1=new Worker("zhang3",18,3000);
Worker w2=new Worker("li4",25,3500);
Worker w3=new Worker("wang5",22,3200);
Worker w4=new Worker("zhao6",24,3300);
list.add(w1);
list.add(w2);
list.add(w3);
list.add(1,w4);
list.remove(3);
System.out.println(list.get(0));
for(int i=0;i<list.size();i++) {
Worker wor=(Worker)list.get(i);
System.out.println(wor.getName());
wor.work();
}
}
}
class Worker{
private String name;
private int age;
private double salary;
public Worker() {}
public Worker(String name, int age, double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
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;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void work() {
System.out.println(name+"work");
}
public boolean equals(Worker work) {
// TODO Auto-generated method stub
if(this==work) {
return true;
}
if(work==null) {
return false;
}
if(this.getClass()!=work.getClass()) {
return false;
}
if(this.name.equals(work.name)&&this.age==work.age&&this.salary==work.salary){
return true;
}
return false;
}
}