Java Collection 接口、Set 接口、List 接口基本操作 练习

时间:2022-11-20 13:27:19
////////  One
package Chp11; import java.util.ArrayList;
import java.util.List; public class TestListOne
{
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
list.add("Hello");
list.add("World");
list.add(1,"Learn");
list.add(1,"Java"); PrintList(list);
} public static void PrintList(List<String> list)
{
System.out.println(list);
} }
/////////////////////////////////////////////////////////////////
// Two
package Chp11; import java.util.ArrayList;
import java.util.List; public class TestJavaTwo
{
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
list.add("Hello");
list.add("World");
// list.add("World");
list.add("Hello");
list.add("Learn");
list.remove("Hello");
list.remove(0); for (int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i));
}
}
} ////////////////////////////////
/// Three
/*
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.name = name;
this.age = age;
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary(){
return salary;
}
public void setSalary(double salary){
this.salary = salary;
}
public void work(){
System.out.println(name + “ work”);
}
}
完成下面的要求
1) 创建一个List,在List 中增加三个工人,基本信息如下:
姓名 年龄 工资
zhang3 18 3000
li4 25 3500
wang5 22 3200
1) 插入一个工人,信息为:姓名:zhao6,年龄:24,工资3300
2) 删除wang5 的信息
3) 遍历,对List 中所有的工人调用work 方法。 */
// 答案:
public class TestListThree
{
public static void sop(List<Worker> workers)
{
System.out.println("Name Age Sallary");
/*
第一种:
for (int i = 0; i < workers.size(); i++)
{
System.out.print(workers.get(i).getName()+" ");
System.out.print(workers.get(i).getAge()+" ");
System.out.println(workers.get(i).getSalary());
}
*/ // 第二种方法
for (Worker worker : workers)
{
System.out.print(worker.getName()+" ");
System.out.print(worker.getAge()+" ");
System.out.println(worker.getSalary());
}
}
public static void main(String[] args)
{
List<Worker> workers = new ArrayList<Worker>();
workers.add(new Worker("zhang3", 18, 3000));
workers.add(new Worker("li4", 25, 3500));
workers.add(new Worker("wang5", 22, 3200)); sop(workers); System.out.println("=====================");
workers.add(new Worker("zhao6",22,3300));
sop(workers);
System.out.println("=====================");
workers.remove(2);
sop(workers);
}
} /*
题目: (Map)已知某学校的教学课程内容安排如下: 老师 课程
Tom CoreJava
John Oracle
Susan Oracle
Jerry JDBC
Jim Unix
Kevin JSP
Lucy JSP
完成下列要求:
1) 使用一个Map,以老师的名字作为键,以老师教授的课程名作为值,表示上述课程安排。
2) 增加了一位新老师Allen 教JDBC
3) Lucy 改为教CoreJava
4) 遍历Map,输出所有的老师及老师教授的课程
5) *利用Map,输出所有教JSP 的老师。 */ // 答案如下: package Chp11; import java.util.HashMap;
import java.util.Map;
import java.util.Set; public class TestMapFour
{ /**
* @param args
*/
public static void main(String[] args)
{
Map<String, String> map = new HashMap<String, String>();
map.put("Tom", "CoreJava");
map.put("John", "Oracle");
map.put("SuSan", "Oracle");
map.put("Jerry", "Jdbc");
map.put("Jim", "Unix");
map.put("Kevin", "Jsp");
map.put("Lucy", "Jsp"); // 1.增加
map.put("Allen", "JDBC");
// 2.修改
map.put("Lucy", "CoreJava ");
// 3.遍历
Set<String> set = map.keySet();
for (String s : set)
{
String value = map.get(s); // 通过键取值
System.out.println(s+" 教的课程为 "+value); // 4.教JSP的老师
if ("JSP".equals(value))
{
System.out.println(s+" 教的课程为 "+value);
}
} } }