当前位置: 移动技术网 > IT编程>开发语言>Java > Java 基础详解(泛型、集合、IO、反射)

Java 基础详解(泛型、集合、IO、反射)

2019年07月19日  | 移动技术网IT编程  | 我要评论
计划把 java 基础的有些部分再次看一遍,巩固一下,下面以及以后就会分享自己再次学习的一点笔记!不是有关标题的所有知识点,只是自己觉得模糊的一些知识点。 1.对于泛型类

计划把 java 基础的有些部分再次看一遍,巩固一下,下面以及以后就会分享自己再次学习的一点笔记!不是有关标题的所有知识点,只是自己觉得模糊的一些知识点。

1.对于泛型类而言,你若没有指明其类型,默认为object;

2.在继承泛型类以及接口的时候可以指明泛型的类型,也可以不指明;

3.泛型也数据库中的应用:

写一个 dao 类对数据库中的数据进行增删改查其类型声明为 <t> 。每张表对应一个类,对应每一张表实现一个类继承该 dao 类并指明 dao 泛型为该数据表对应的类,再实现一个与该表匹配的 dao 操作类,这样就不必在每一个数据表的操作实现类中去实现增删改查的基本方法。例如(实际应用中大概就是这思想,下面的举例并不完整):

//数据表对应的类
public class customer{
 private int id;
 private string name;
 ...
}

//所有数据表的操作类都要实现的 dao 基类
public class dao<t> {
 //增
 public void add(t t) {
 …
 }
}

public t get(int index) {
 //查
 return null;
}

public void delete() {
 //删
 …
}

public list<t> getforlist(int index) {
 //查
 return null;
}

//数据表操作对应的实现类
public class customerdao extends dao<customer> {
    
}

//测试类
public class test {
 public static void mian(string[] args) {
 customerdao cus = new customerdao;
 cus.add(new customer);
 }
}

4. 静态方法中不可以使用泛型(static)

因为static 声明的方法或者类以及变量都是在类初始化的时候初始化,而泛型是在运行的时候才回去初始化的,所以就出现了问题(后出现的调用了先出现的)。

public t t;
 //error
 public static void show() {
  system.out.println(t);
}

5.通配符

可以读取声明为通配符的集合,但不可往里写入元素(读的时候可以把元素都认为是 object,但写的时候其声明为 ?,不是 object,也就是说写什么类型都是错的,但可以存 null),例如

public void testlist() {
 list<string> strlist = new arraylist<>();
 strlist.add(“hello”);
 strlist.add(“world”);

 //correct
 list<?> list = strlist;
 
 //error
 list.add(“hi”);
 list.add(123);
 //correct
 list.add(null);
}

6.集合map 的遍历

package com.java.map.test;

import java.util.arraylist;
import java.util.collection;
import java.util.collections;
import java.util.hashmap;
import java.util.iterator;
import java.util.list;
import java.util.map;
import java.util.set;

public class mapentry {

 public static void main(string[] args) {
  map<integer, string> map = new hashmap<>();
  map.put(1, "a");
  map.put(2, "b");
  map.put(3, "c");
  map.put(4, "d");
  map.put(5, "e");
  // 得到 map 所有键的集合
  set<integer> keys = map.keyset();

  for (integer key : map.keyset()) {
   system.out.println(map.get(key));
  }

  // 利用迭代器 遍历
  iterator<map.entry<integer, string>> it = map.entryset().iterator();

  while (it.hasnext()) {
   map.entry<integer, string> entry = it.next();
   system.out.println(entry.getkey() + " -> " + entry.getvalue());
  }

  // 第三种
  for (map.entry<integer, string> entry : map.entryset()) {
   system.out.println(entry.getvalue());
  }

  // 遍历所有的 value 值
  collection<string> values = map.values();

  for (string val : values) {
   system.out.println(val + ">>-");
  }

  iterator<string> i = values.iterator();
  while (i.hasnext()) {
   system.out.println(i.next() + "-->");
  }

  list<string> lists = new arraylist<>();
  lists.add("1");
  lists.add("2");
  lists.add("3");
  lists.add("4");

  iterator<string> it2 = lists.iterator();

  while (it2.hasnext()) {
   system.out.println(it2.next());
  }
  
  collections.reverse(lists);
  iterator<string> it3 = lists.iterator();
//  comparator comparator = new 
  
  
  while (it3.hasnext()) {
   system.out.println(it3.next() + "<->");
  }
 }
}

7.利用反射获取方法名和属性名,利用反射还可以获取构造器等其他信息

package com.java.reflct.test;

//实体类
public class person {

 private string id;
 
 private string name;
 
 public int phone;
 
 public void setid(string id) {
  this.id = id;
 }
 
 public string getid() {
  return id;
 }
 
 public void setname(string name) {
  this.name = name;
 }
 
 public string getname() {
  return name;
 }
 
 public void setphone(int phone) {
  this.phone = phone;
 }
 
 public int getphone() {
  return phone;
 }
 
 private void print() {
  system.out.println("your id is " + id + ", your name is " + name + ", your phone is " + phone + "!");
 }

 @override
 public string tostring() {
  return "person [id=" + id + ", name=" + name + ", phone=" + phone + "]";
 }
}

package com.java.reflct.test;
//测试类

import java.lang.reflect.field;
import java.lang.reflect.invocationtargetexception;
import java.lang.reflect.method;

public class testreflect {

 public static void main(string[] args) {
  try {
//   通过 class.forname("全类名"); 获取 class,还以利用 对象名.getclass() 类名.class(); 获取class
   class cla = class.forname("com.java.reflct.test.person");
   class cla2 = person.class;
   
//   获取所有的 变量,返回数组,包括私有变量
   field[] fields = cla2.getdeclaredfields();
//   遍历变量数组
   for (field fie : fields) {
    system.out.println(fie+"-..-");
   }
   
//   获取所有的方法,返回数组,包括私有方法
   method[] methods = cla.getdeclaredmethods();
   
   for (method met : methods) {
    system.out.println(met);
   }
   
   try {
//    获取单个私有属性
    field field = cla.getdeclaredfield("id");
//    打破封装
    field.setaccessible(true);
    system.out.println(field + "<<>>");
   } catch (nosuchfieldexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   } catch (securityexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   }
   
   method method = null;
   
   try {
//    获取单个私有方法
    method = cla.getdeclaredmethod("print");
//    打破封装
    method.setaccessible(true);
    system.out.println(method + ">><<");
   } catch (nosuchmethodexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   } catch (securityexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   }
   
   try {
//    通过cla.newinstance(); 获取类的对象
    person person = (person) cla.newinstance();
    person.setid("1");
    person.setname("yinyin");
    person.setphone(110);
    
    system.out.println(person + "__>>__");
    try {
//     执行 person 对象的中 method 所对应的方法
     method.invoke(person);
    } catch (illegalargumentexception e) {
     // todo auto-generated catch block
     e.printstacktrace();
    } catch (invocationtargetexception e) {
     // todo auto-generated catch block
     e.printstacktrace();
    }
   } catch (instantiationexception e1) {
    // todo auto-generated catch block
    e1.printstacktrace();
   } catch (illegalaccessexception e1) {
    // todo auto-generated catch block
    e1.printstacktrace();
   }
   
   
  } catch(classnotfoundexception e) {
   system.out.println("there is no class " + e);
  }
 }
}

8.comparator  类的使用(利用  comparator  实现集合的自定义排序)

注意区分 collections (集合的处理类)和 collection (集合基类)

package com.java.collection.test;

import java.util.arraylist;
import java.util.collections;
import java.util.comparator;
import java.util.iterator;
import java.util.list;

/*
 * collections 是 collection 的操作类
 */

public class collectioncomparator {
 
 public static void main(string[] args) {
  
  comparator<customer> com = new comparator<customer>(){

   @override
   public int compare(customer o1, customer o2) {
//    将 id 的比较值放入参数中,使得 id 相等时有其他的处理方法
    int i = o1.getid().compareto(o2.getid());
    
//    当 id 相等的时候比较名字的顺序
    if (i == 0) {
//     给 return 添加一个 - 号可以实现 “从大到小”
     return o1.getname().compareto(o2.getname());
    }
//         id 不相等时返回其值  
         return i;
   }
  };
  
  list<customer> lists = new arraylist<>();
  lists.add(new customer("yinyin", "110", 1001));
  lists.add(new customer("zhao", "10086", 1002));
  lists.add(new customer("ls", "10010", 1001));;
  
  collections.sort(lists, com);
  
//  利用匿名类实现自定义排序
  /*
  collections.sort(lists, new comparator<customer>(){

   @override
   public int compare(customer o1, customer o2) {
//    将 id 的比较值放入参数中,避免 id 相等没有其值
    int i = o1.getid().compareto(o2.getid());
    
//    当 id 相等的时候比较名字的顺序
    if (i == 0) {
     return o1.getname().compareto(o2.getname());
    }
    return i;
   }
  });
  */
 //利用迭代器遍历集合
  iterator<customer> it = lists.iterator();
  while(it.hasnext()) {
   system.out.println(it.next());
  }
  
 }
}

9.io

读取目标文本文件的字节数

package com.java.io.file.test;

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.io.inputstream;

public class myiotest {

 public static void main(string[] args) {
  
//  在 io 中出现的异常最好都使用 try-catch 包裹起来,不要 throw,因为这样可以保证流的关闭在任何时候都可以正常执行
  inputstream filestream = null;
  int count = 0;
  try {
   filestream = new fileinputstream(new file("hello.txt"));
//   读取文件的下一个字节
   while (filestream.read() != -1) {
    count++;
   }
//   打印目标文件的字节数
   system.out.println(count);
  } catch (filenotfoundexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (ioexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } finally {
   try {
    filestream.close();
   } catch (ioexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   }
  }
 }
}

实现文件的复制(inputstream 、outputstream 和 reader 、writer)。文本文件的操作使用 reader writer(字符流) 去实现,效率高。但是不可以去操作媒体文件;媒体文件使用 inputstream outputstream 去实现,也可以对文本文件进行操作,但是没有字符流高效。

package com.java.io.file.test;

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;

public class copyfile {

 public static void main(string[] args) {
//  设置一次从目标文件中读取多少字节
  byte[] buffer = new byte[1024];

  int len = 0;
  file file = new file("c:/users/lenovo/desktop/123.wmv");
  inputstream fileinput = null;
  outputstream fileout = null;

  try {
   fileinput = new fileinputstream(file);
   fileout = new fileoutputstream(new file("c:/users/lenovo/desktop/trave2.wmv"));

//   len 的作用是防止读取文件时最后一次其长度不够读取被置为零,read() 返回读入缓冲区的字节总数
   while ((len = fileinput.read(buffer)) != -1) {
    fileout.write(buffer, 0, len);
   }
   system.out.println("succ");
  } catch (filenotfoundexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (ioexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } finally {
   try {
    fileout.close();
    fileinput.close();
   } catch (ioexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   }
  }
 }

}





//利用 reader writer 实现
package com.java.io.file.test;

import java.io.file;
import java.io.filenotfoundexception;
import java.io.filereader;
import java.io.filewriter;
import java.io.ioexception;
import java.io.reader;
import java.io.writer;

public class readerwriter {

 public static void main(string[] args) {
  file file = new file("hello.txt");
  int len = 0;
  reader filereader = null;
  writer filewriter = null;
  char[] ch = new char[125];
  
  try {
   filereader = new filereader(file);
   filewriter = new filewriter(new file("world.txt"));
   
   while((len = filereader.read(ch)) != -1) {
    filewriter.write(ch, 0, len);
   }
  } catch (filenotfoundexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (ioexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } finally {
   try {
    filewriter.close();
    filereader.close();
   } catch (ioexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   }
  }
 }
}

10.利用缓冲流实现文件的复制操作,效率更高

package com.java.io.file.test;

import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;

public class testbufferedcopy {
// 使用缓冲流实现文件的复制
 public static void main(string[] args) {
  
  int len = 0;
  byte[] buffer = new byte[2048];
  
  file file = new file("c:/users/lenovo/desktop/123.rmvb");
  inputstream inputfile = null;
  outputstream outputfile = null;

  bufferedinputstream bufferedinput = null;
  bufferedoutputstream bufferedoutput = null;
  
  try {
   inputfile = new fileinputstream(file);
   outputfile = new fileoutputstream("c:/users/lenovo/desktop/456.rmvb");
   
   bufferedinput = new bufferedinputstream(inputfile);
   bufferedoutput = new bufferedoutputstream(outputfile);
   
   while((len = bufferedinput.read(buffer)) != -1) {
    bufferedoutput.write(buffer, 0, len);
   }
   
   system.out.println("succ");
  } catch (filenotfoundexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (ioexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } finally {
   try {
//    只需关闭复制文件用到的就可以,即最后两个
    bufferedoutput.close();
    bufferedinput.close();
   } catch (ioexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   }
  }
 }
}

上面的代码总结啥的都是自己平常练习过程中的代码和心得,对于知识点讲解覆盖的并不全面,还望谅解。初来乍到不知道该如何去写!

以上这篇java 基础详解(泛型、集合、io、反射)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网