在集合中使用泛型:
1.集合接口或集合类在jdk5.0时都修改为带泛型的结构
3.指明完以后,在集合类或接口中凡是定义类或接口时,内部结构(例如:方法、构造器/属性等)使用到类的泛型的位置,都指定为实例化的泛型类型。例如:add(E e)—>实例化后:add(Integer e)
4.注意点:泛型的类型必须是类,不能是基本数据结构,需要用到基本数据类型的位置,拿包
5.若实例化时,没有指明泛型的类型,默认类型为java.lang.Object类型
package shangGuiGu.day28; import org.junit.Test; import java.time.Instant; import java.util.*; public class demo01 { @Test public void test(){ ArrayListints = new ArrayList (); //编译时,就会进行类型检查,保证数据的安全 ints.add(47); ints.add(45); ints.add(25); ints.add(90); for (Integer score:ints){ int stuscore=score; System.out.print(stuscore); } System.out.println(); Iterator iterator=ints.iterator(); while(iterator.hasNext()){ int stuscore=iterator.next(); System.out.print(stuscore); } } @Test public void test1(){ Map map=new HashMap (); map.put("dd",24); map.put("fd",3); Set > entry= map.entrySet(); Iterator > iterator = entry.iterator(); while(iterator.hasNext()){ Map.Entry next = iterator.next(); String key=next.getKey(); Integer value = next.getValue(); System.out.println(key+"--->"+value); } } }
自定义泛型结构:泛型类、泛型接口、泛型方法
package shangGuiGu.day28; //自定义泛型类 public class demo02{ String ordername; int orderid; //类的内部结构可以使用类的泛型 T orderT; public demo02(){ } public demo02(String ordername,int orderid,T orderT){ this.ordername=ordername; this.orderid=orderid; this.orderT=orderT; } public T getOrderT(){ return orderT; } public void setOrderT(T orderT){ this.orderT=orderT; } @Override public String toString() { return "demo02{" + "ordername='" + ordername + ''' + ", orderid=" + orderid + ", orderT=" + orderT + '}'; } }
package shangGuiGu.day28; import org.junit.Test; public class demo03 { @Test public void test(){ //若定义了泛型类,实例化没有指明类的泛型,则认为此泛型类型为Object类型 //要求:若定义了类是带泛型的,建议在实例化时要指明类的泛型 demo02 demo02 = new demo02(); demo02.setOrderT(123); demo02.setOrderT("abc"); //建议:实例化时指明类的泛型 shangGuiGu.day28.demo02stringdemo02 = new demo02 ("orderaa",1991,"dd"); stringdemo02.setOrderT("aa:hello"); } @Test public void test2(){ demo04 sub1=new demo04(); //由于子类在继承带泛型的父类时指明了泛型类型。则实例化子类对象时,不再需要指明泛型类 sub1.setOrderT(1122); } }
package shangGuiGu.day28; public class demo04 extends demo02{ }
package shangGuiGu.day28; import org.junit.Test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class demo06 { //泛型在继承方面的体现 //类A是类B的父类,G和G不具有子父类关系,二者是并列关系 //类A是类B的父类,A是B 的父类 @Test public void test1(){ Object obj=null; String str=null; obj=str; Object[] arr1=null; String[] arr2=null; arr1=arr2; List
package shangGuiGu.day28; import java.util.*; public class demo07{ private Map map=new HashMap (); //保存T类型的对象到Map成员变量中 public void save(String id,T entity){ map.put(id,entity); } //从map中获取id对应的对象 public T get(String id){ return map.get(id); } //替换map中key为id的内容,改为entity对象 public void update(String id,T entity){ if(map.containsKey(id)){ map.put(id,entity); } } //返回map中存放的所有T 对象 public List list(){ List list=new ArrayList<>(); Collection values=map.values(); for(T t:values){ list.add(t); } return list; } //删除指定id对象 public void delete(String id){ map.remove(id); } }
package shangGuiGu.day28; import java.util.Objects; public class demo08 { private int id; private int age; private String name; public demo08() { } public demo08(int id, int age, String name) { this.id = id; this.age = age; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } @Override public String toString() { return "demo08{" + "id=" + id + ", age=" + age + ", name='" + name + ''' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; demo08 demo08 = (demo08) o; return id == demo08.id && age == demo08.age && Objects.equals(name, demo08.name); } @Override public int hashCode() { return Objects.hash(id, age, name); } }
package shangGuiGu.day28; import shangGuiGu.day9.demo01; import java.util.List; public class demo09 { public static void main(String[] args) { demo07IO流dao=new demo07 (); dao.save("1001",new demo08(1001,34,"zhou")); dao.save("1002",new demo08(1002,20,"zho")); dao.save("1003",new demo08(1003,24,"zh")); dao.update("1002",new demo08(1002,20,"zoo")); dao.delete("1003"); List list=dao.list(); list.forEach(System.out::println); } }
创建File类的实例
File(String filePath)
File(String parentPath,String ChildPath)
File(File parentPath,String ChildPath)
package shangGuiGu.day28; import org.junit.Test; import java.io.File; public class demo10 { @Test public void test1(){ //构造器1 //相对路径:相较于某个路径下,指明的路径 File file1=new File("hello.txt");//相对于当前module //绝对路径:包含盘符在内的文件或文件目录的路径 File file2=new File("C:\Users\王誉潼\Desktop"); System.out.println(file1); System.out.println(file2); //构造器2 File file3=new File("C:\Users\王誉潼","Desktop"); //构造器3 File file4=new File(file3,"hi.txt"); System.out.println(file4); } @Test public void test2(){ File file1=new File("hello.txt"); File file2=new File("C:\Users\王誉潼\Desktop\hi.txt"); System.out.println(file1.getAbsoluteFile()); System.out.println(file1.getPath()); System.out.println(file1.getName()); System.out.println(file1.getParent()); System.out.println(file1.length()); System.out.println(file1.lastModified()); System.out.println(); System.out.println(file2.getAbsoluteFile()); System.out.println(file2.getPath()); System.out.println(file2.getName()); System.out.println(file2.getParent()); System.out.println(file2.length()); System.out.println(file2.lastModified()); } @Test public void test3(){ File file=new File("C:\Users\王誉潼\Desktop\hi.txt"); String[] list=file.list(); for(String s:list){ System.out.println(s); } System.out.println(); File[] files=file.listFiles(); for(File f:files){ System.out.println(f); } } @Test //要想保证返回TRUE,需要file1在硬盘中是存在的,file2在硬盘中不存在 public void test5(){ File file1=new File("dhi.txt"); File file2=new File("C:\Users\王誉潼\Desktop\hi.txt"); boolean renameto=file1.renameTo(file2); System.out.println(renameto); } }
package shangGuiGu.day28; import org.junit.Test; import java.io.File; import java.io.IOException; public class demo11 { @Test public void test(){ File file1=new File("dhi.txt"); System.out.println(file1.isDirectory()); System.out.println(file1.isFile()); System.out.println(file1.exists()); System.out.println(file1.canRead()); System.out.println(file1.canWrite()); System.out.println(file1.isHidden()); File file2=new File("C:\Users\王誉潼\Desktop\hi.txt"); System.out.println(file2.isDirectory()); System.out.println(file2.isFile()); System.out.println(file2.exists()); System.out.println(file2.canRead()); System.out.println(file2.canWrite()); System.out.println(file2.isHidden()); } @Test public void test1() throws IOException { File file1=new File("dhi.txt"); if(!file1.exists()){ file1.createNewFile(); System.out.println("创建成功"); }else{ file1.delete(); System.out.println("删除成功"); } } @Test public void test3(){ File file1=new File("d:\io\io1"); boolean mkdir=file1.mkdir(); if(mkdir){ System.out.println("chenggong1 "); } File file2=new File("d:\io\io2"); boolean mkdir1=file2.mkdirs(); if(mkdir1){ System.out.println("chenggong2 "); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)