java类集合

来源:互联网 发布:手机端图片点击放大js 编辑:程序博客网 时间:2024/06/08 14:10
public class KindMode {
public static void main(String[] args) throws IOException {
//单链表的基本操作
LinkedList link=new LinkedList();
link.add(0, "shxiinfa");
link.add(1,"zhangyan");
link.add(2,"shixiaocun");
link.remove(1);
System.out.println(link);
//和队列是一样的,去除头部
link.pop();
System.out.println(link);
link.peek();
System.out.println(link);
//栈的基本操作
Stack<Integer> stack=new Stack<Integer>();
stack.add(1);
stack.add(2);
stack.add(0,22);
stack.pop();
System.out.println(stack);
//队列的基本操作
//Queue<Integer>  queue=new Queue<Integer>();







//link.add(3, "yejianhua");
//link.peek();
// 添加基础类型数据
ArrayList<Integer> list = new ArrayList<Integer>();
int[] a = { 1, 2, 4, 6, 7 };
int[] b = new int[12];
list.add(1);
list.add(2);
System.out.println(list);
// 添加字符串
StringBuffer sb = new StringBuffer();
String s[] = { "shixinfa2", "zhangyan" };
sb.append("shixinfa");
sb.append(s).toString();
System.out.println(sb);
// 往文件中输入字符串
File mkfile = new File("D://bb.txt");
if (!mkfile.exists()) {
mkfile.createNewFile();
}
FileOutputStream input = new FileOutputStream(mkfile);
String context = "this is a page";
byte by[] = new byte[1024];
by = context.getBytes();
input.write(by);
input.close();

BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容,一次读一整行:");
            reader = new BufferedReader(new FileReader(mkfile));
            String tempString = null;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
/*FileInputStream readfile = new FileInputStream(mkfile);

* int recevie; recevie=readfile.read(by); while(recevie!=-1){
* System.out.println(recevie); } readfile.close(); //
 
int tempbyte;
while ((tempbyte = readfile.read()) != -1) {
System.out.write(tempbyte);
}
readfile.close();
}
*/
//单链表的基本操作