Java高级编程6-姜国海 集合 输入输出

①List

add 
get
isEmpty
remove
set
subList
toArray

实现类:
linkList.

map集合

HaseMap
TreeMap

put 添加
get
clear
keySet
values
remove

Eg:
import java.

Map map = new TreeMap();
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
map.put("001",s1);
map.put("001",s2);
map.put("001",s3);
map.put("001",new Student());//返回值是s3

Iterator it = map.iterator();
while(it.hasNext()){
    Object o = it.next();
}

遍历:
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=0;i<list.size();i++){
    System.out.println(list.get(i));
}

TreeSet : 要求元素可以比较大小,所以必须实现Comparable接口

Eg:
public class Student implement Comparable {
//meta-class 描述类的类 元类
//meta-database 描述数据库的数据库
//meta-data

    public boolean equals(Object o){
        if(o.getClass() == Student.class){
            Student s = (Student)o;
            return this.id.equals(s.id);//this 可以不写

            //或者

            return this.age == s.age &&
                   this.gender == s.gender &&
                   this.id.equals(s.id);//这里的id是计较指向的字符串而不是引用变量所以用equals
        }else{
            return false;
        }
    }

    public int compareTo(Object o){
        return this.age - ((Student)o).age;
    }
}

HaseSet 可以插入不同类型的元素
set


Map map = new HaseMap();
map.put(new Integer(3),"001");
map.put(new Integer(6),"001");
map.put(new Integer(5),"001");
map.put(new Integer("6"),"001");//此时map.size()==3 本行执行了一个替换

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);

}

Hasetable类 小写的t 拒绝null值


数据的输入输出

Package java.io
方向
input output 输入输出流
类型
字节流 字符流
字符流 字节长度不固定,依据于数据的编码方式

Stream结尾的是字节流

public abstract class InputStream extends Object
InputStream 是抽象类

InputStream成员函数 :
int read();//读下一个字节的 当返回-1 表明流中没有数据 0-255 是正常的数据
int read(byte[] s);读一个字节数组  返回值为读入的数据的长度,字节为单位
int read(byte[],int off,int len); 读入的数据放入数组中对应的位置
close();关闭流 谁打开的流谁关闭 如果其他的程序传入的流 不要关闭 因为之后可能还要用
markSupported();
mark() ;//在流上标记
reset();回到做mark的位置,继续读
long skip(long n);//越过几个字节


具体的类
FileInputStream();

输出流:

write(int b) 输入整数的最低位的一个字节
write(byte[] b)//输出中所有的数据
write(byte[] b,int off ,int len)//从off开始输出长度为len的数据
flush();//强制输出缓冲区中的内容
FileOutputStream();

Eg:

import java.io.* ;
public class Copy2{
    public static void main (String[]  args) {
        try {
            FileInputStream fis =  new FileInputstream(args[0]) ;
            FileOutputStream fos = new  FileOutputStream(args [1]); 
            int c=  0;
            byte[] buffer = new byte[8192]; 1/设置字节数组作为缓
            while ((c = fis.read (buffer)) != -1){一次读入一个
                fos. write (buffer, 0,c); /读入多少数据则输出多少
            }
          fis.close() ;
          fos.close () ;  
        } catch  (Exception e){
          e .printStackTrace () ;
        }
    }
}

字符流

read();//

windows 下 type 文件名
输出文本文件打印输出到控制台

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

分类:节点流和处理流

扫描二维码关注公众号,回复: 2426167 查看本文章

节点流:一个具体的流结点
处理流:传入的是其他的流对象

以流操作执行对数组的数据的读写操作
ByteArrayInputStream(byte[] buf);

int readInt(){
    FileInputStream fis = new FileInputStream("aa.dat");
    int i1 = fis.read();
    int i2 = fis.read();
    int i3 = fis.read();
    int i4 = fis.read();
    int i = i1 | i2<<8 | i3 <<16 | i4<<24;
    return i;
}

流链接

int readInt(){
    FileInputStream fis = new FileInputStream("aa.dat");
    DataInputStream dis = new DataInputStream(fis);

    String 

    return i;
}
import java.io.*;

public class WriteData {
    public static void main(){
        FileOutputStream fos = new FileOutputStream(args[0]);
        DataOutputStream dos = new DataOutputStream(fos);

        dos.writeInt(5);
        dos.writeDouble(5.14);
        dos.writeUTF("中国梦");
        dos.writeUTF("us dream");
        dos.close();
        fos.close();
    }

}

深复制与浅复制

import java.io.*;
public class Student implements java.io.Serializable{//声明为可以序列化的类
    Watch w1;//类中的所有的成员都要声明为可序列化的接口
}
FilterInputStream 是一切处理流根类

代理
public class FilterInputStream extends InputStream{
}

猜你喜欢

转载自blog.csdn.net/l1558198727/article/details/80964839
今日推荐