JAVA笔记(6)集合,comparable接口,IO

list

remove(int index);
remove(Object o);
set();

ArrayList LinkedList
TreeSet
在TreeSet中的元素都要实现comparable接口
map
TreeMap
HashMap

添加put();
获取get();

返回key构成的set集合KeySet();
返回值的集合:

values();

使用集合要导入java.util.*

两次put同一值对应的映射,前者被覆盖

集合的遍历

iterator it=c.iterator();
while(it.hashNext()){
    Object o=it.next;
    visit(o);

}
List list = new list();
list .add(new Integer(3));
liat.add("5");
list.add(long(5));
iterator it = list.iterator();
while(it.hashNext()){
    System.out.println(it.next());
}
for(int i=1;i<list.size();i++){
    System.out.println(list.get(i));
}
List list = new TreeSet();
list.add(new Integer(3));
liat.add("5");//会抛出异常:ClassCastExcepton(内部元素要可以比较大下)。可以抛出
list.add(long(5));
iterator it = list.iterator();
while(it.hashNext()){
    System.out.println(it.next());
}
for(int i=1;i<list.size();i++){
    System.out.println(list.get(i));
}
List list = new HashSet();//不能添加相同的元素就行。对象类型可以不同
list.add(new Integer(3));
liat.add("5");//会抛出异常:ClassCastExcepton(内部元素要可以比较大下)。可以抛出
list.add(long(5));
iterator it = list.iterator();
while(it.hasNext()){
    System.out.println(it.next());
}
for(int i=1;i<list.size();i++){
    System.out.println(list.get(i));
}
Set map=new HashMap();
map.put(new Intager(5),"001");
map.put(new Intager(6),"001");
map.put(new String("5"),new Intager(5));
map.put(new Intager("6"),new Student());
//遍历:
Set set=map.KeySet();
iterator it=set.iterator();
while(it.hasNext()){
    Object key=it.next();
    Object value=map.get(key);
    System.out.println("key , "+ value);
}

comparable接口


equals(){
    if(o.getClass() == Student.class){//对象.getClass(),类.Class相等。
        Student s = (Student) o ;
        return this.id==o.id&&this.age==o.age;//this.可以省略
    }
    else{
        return false;
    }
}
public int compareTo(Object o){//从小到大
    return this.age -((Student)o).age;//按年龄排序。颠倒两项可以变为从大到下
}

hashTable尽量少使用。因为其中的值不能为null;为老版本的Java中的类。

IO
import Java.io.*

数据流:输入流、输出流
类型分:字节流、字符流

InputStream //stream都是字节流
    int read();//如果正确则从0-255 如果是-1则流结束 读一个byte返回类型为int 返回数据具体的值 
    int read(byte[] b);//返回读取到的数据长度
    int read(byte[] b, int off, int len);//将数据读出后放到对应数组中对应的位置。
    void close();//流对象不要随便关闭,最好是谁打开的谁关闭。

    void mark(); //标记。
    void reset();//返回标记。部分流支持mark 和 reset函数

    long skip(long n);//越过n个字节


OutputStream
    void write(int b);//输出整数最低位的一个字节。如果要输出整数要进行移位处理。
    void write(byte[]b);//将数组添加到输出流之中。
    void write(byte[]b,int off, int len);//从off开始输出长度为len的数据
    void flush();//强制输出缓冲区中的数据。

Class FileInputStream//使用文件路径字符串来构造流。
    InputStream in = new FileInputStream(String s);
Class FileOutputStream

**e.printStackTrace();打印错误信息到控制台。

Reader字符读
    FileReader();

Writer字符写

    write(char[] buf ,int off);
public class type{
    public static void main(String[] args) throws exception{
        FileReader fr=new FileReader(args[0]);
        int c;
        while(true){
            c=fr.read();
            if(c==-1){
                break;
            }
            System.out.print((char)c);
        }
    }
}

分类节点流和处理流

Class ByteArrayInputStream;
    ByteArrayInputStream(byte[] b);//设备
Class ByteAttayOutputStream;

处理流:提供更高层次的封装
数据输入流

Class DataInputStream;
    readInt();
    readLine();

编写函数 将int存入流中然后再取出。
java先发低位

int i1=fis.read();
int i2=fis.read();
int i3=fis.read();
int i4=fis.read();
//位运算拼接

传输大文件:将文件或者图片转化为对应的字节流传输
流链接 Streamchain

FileInputStream fis=new..;
DataInputStream dis=new...;

Class writeData{
    public static void main(String[]args){
        FileOutputStream fos=new FileOutputStream("a.zip");
        ZipOutputStream zos=new ZipOutputStream(fos);
        DataOutputStream dos=new DataOutputStream(zos);
        dos.wirteInt(5);
        dos.writeDouble(5.14);
        dos.writeUTF("中国梦");
        dos.writeUTF("US Dream");
        dos.close();
        fos.close();
    }
}

用流来实现深复制:

ByteArrayOutputStream baos=new ...;
ObjectOutputStream oos = new ...(oos);
oos.writeObject(s);
ByteArrayInputStream bais=new...();
ObjectInputStream ois=new .....(bais);
Student s = (Student)ois.readObject();
baos.close();
public class Student implements java.io.Serializable{
    Watch w1;
}
FilterInputStream{//代理
    private DataInputStream in;
    public FilterInputStream(....in ){
            this.in=in;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值