1.掌握集合类的概念
2.掌握Set、List、Map三个集合的区别,。
3.掌握使用迭代器进行集合的遍历;
1.定义一个TestList类要求如下
(1)利用List接口和泛型新建一个ArrayList对象list,要求为String类型;
(2)list添加“China”,“Usa”,“India”,“Japan”四个字符串;
(3)打印输出list
(4)用四种方式遍历list元素
(5)删除索引为2的元素,然后打印输出list;
(6)查阅api文档和互联网资料,利用Collections的sort()方法对list进行字典排序,然后打印输出结果
2.定义一个类TestSet要求如下
(1)利用Set接口和泛型新建一个HashSet对象set,要求为String类型
(2)set依次添加"黄山"、“大蜀山”、“泰山”、“大蜀山”、“黄山”;
(3)打印输出set查看结果;
(4)用Iterator遍历set
3.定义一个类TestMap要求如下
(1)利用Map接口和泛型新建一个字典HashMap对象map,然后插入如下键值对
China 中国
India 印度
USA 美国
(2)打印输出key为China的值;
(3)查阅api文档和互联网资料,分别用三种不同的方法对此map对象进行遍历打印输出;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class TestList {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list=new ArrayList<String>();
list.add("China");
list.add("Usa");
list.add("India");
list.add("Japan");
System.out.println(list);
System.out.println("第1种方式遍历");
for(String s:list){
System.out.println(s);
}
System.out.println("第2种方式遍历");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
System.out.println("第3种方式遍历");
Iterator<String> it=list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("第4种方式遍历");
list.forEach(v->System.out.println(v));
list.remove(2);
System.out.println("删除后"+list);
Collections.sort(list);
System.out.println("排序后"+list);
}
}
//2
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class TestSet {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<String> set=new HashSet<String>();
set.add("黄山");
set.add("大蜀山");
set.add("泰山");
set.add("大蜀山");
set.add("黄山");
System.out.println(set);
Iterator<String> it=set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
//3
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String,String> map=new HashMap<String,String>();
map.put("China"," 中国");
map.put("India","印度");
map.put("Usa"," 美国");
String a=map.get("China");
System.out.println(a);
System.out.println("第1种方式遍历");
for(Entry<String,String>j:map.entrySet()){
System.out.println(j.getKey()+j.getValue());
}
System.out.println("第2种方式遍历");
Iterator iter=map.entrySet().iterator();
while(iter.hasNext()){
Entry en=(Entry)iter.next();
String k=(String) en.getKey();
String v=(String) en.getValue();
System.out.println(k+v);
}
System.out.println("第3种方式遍历");
for(String key:map.keySet()){
System.out.println(key+map.get(key));
}
}
}
**掌握
- Java利用流进行文件访问的常见类和常见方法;掌握文件读写的一般方法;掌握JDBC数据库操作方法
- Java中map的使用**
1.定义一个类TestMap要求如下:
(1)新建一个字典HashMap对象(使用泛型),然后插入如下键值对,其中key为整型,value为字符串
1 Apple
2 Orange
3 Pear
(2)删除key值为3的元素;
(3)再添加一个键值对{4=Banana };
(4)分别用4种不同的方法对此Map对象进行遍历打印输出
2.定义一个TestFile类要求如下:
(1) 利用java.nio.file.Files和Path、Paths类在D:\myfile路径下创建文件test.txt;
(2)向该文件写入字符串“我喜欢Java”;
(3)从test.txt文件中读取字符串,并打印输出;
(4)按照步骤1再创建一个文件test2.txt,并向该文件写入字符串“I love java”;
(5)删除文件test2.txt
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry
public class TestMap {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<Integer,String> m=new HashMap<Integer,String>();
m.put(1, "Apple");
m.put(2, "Orange");
m.put(3, "Pear");
m.remove(3);
m.put(4, "Banana");
System.out.println("第1种方式遍历");
for(Integer key:m.keySet()){
System.out.println(key+" "+m.get(key));
}
System.out.println("第2种方式遍历");
Iterator<Integer> it=m.keySet().iterator();
while(it.hasNext())
{
System.out.println(m.get(it.next()));
}
System.out.println("第3种方式遍历");
for(Entry<Integer,String>j:m.entrySet()){
System.out.println(j.getKey());
System.out.println(j.getValue());
}
System.out.println("第4种方式遍历");
m.forEach((k,val)->System.out.println(k+" "+val));
}
}
//2
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TestFile {
public static void main(String[] args) {
Path p = Paths.get("D:\\myfile\\test.txt");
try {
if(!Files.deleteIfExists(p)) {
Files.createFile(p);
}
}catch(IOException e) {
System.out.println(e);
}
try {
Files.write(p,"我喜欢java".getBytes());
}catch(IOException e) {
e.printStackTrace();
}
try {
System.out.println(new String(Files.readAllBytes(p)));
}catch (IOException e) {
e.printStackTrace();
}
Path q = Paths.get("D:\\myfile\\test2.txt");
try {
if(!Files.deleteIfExists(q)) {
Files.createFile(q);
}
}catch(IOException e) {
System.out.println(e);
}
try {
Files.write(q,"i love java".getBytes());
}catch(IOException e) {
e.printStackTrace();
}
try {
if(Files.exists(q)) {
Files.delete(q);
}
}catch(IOException e) {
e.printStackTrace();
}
}
}