java.util.Arrays.sort两种方式的排序(及文件读写练习)

本文介绍了一个使用Java实现的排序示例,包括从文件读取字符串数组并进行排序,同时展示了如何创建自定义比较器及实现Comparable接口来进行对象排序的方法。此外,还介绍了对象序列化和反序列化的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.*;
import java.util.*;
public class SortTest{
   public static void main(String args[]) throws IOException, ClassNotFoundException {
       FileReader InWord = new FileReader(new File("words.txt"));
       BufferedReader in = new BufferedReader(InWord);
       String ws[] = new String[100];
       String input;
       int index = 0;
       while((input=in.readLine())!=null)
           ws[index++]=input;
       Arrays.sort(ws, 0, index);
     
       BufferedWriter out = new BufferedWriter(new FileWriter(new File("SortWords.txt")));
      
       for(String s : ws){
           if(s==null)
             break;
       System.out.println(s);
           out.write(s, 0, s.length());
       out.newLine();
       }
       in.close();
       out.close();
        
       myTest myArray[] = new myTest[20];
       in = new BufferedReader(new FileReader(new File("words.txt")));
       index=0;
       while((input=in.readLine())!=null){
           ws[index++]=input;
       }
       
       for(int i=0; i<index; ++i){
          String str[]=ws[i].split(" ");
          myArray[i] = new myTest(Integer.parseInt(str[0]), str[1]);
          /*
         开始的时候是这样写的, 奥心死了, 作死的节奏啊.....半天没找出来哪里出现了空指针
             myArray[i].x=Integer.parseInt(str[0]);
             myArray[i].name=str[1];
      */
       }
       //1. 利用 自定义的 Comparator类中的compare 方法进行排序
       Arrays.sort(myArray, 0, index, new myComparator());
       //2. 利用 接口Comparable中的compareTo进行排序
       //Arrays.sort(myArray, 0, index);
 
       DataOutputStream dOut = new DataOutputStream(new FileOutputStream(new File("SortWords.txt")));
       for(myTest tmp : myArray){
           if(tmp==nullbreak;
           System.out.println(tmp.x + " " + tmp.name);
           dOut.writeInt(tmp.x);
           dOut.writeChar(' ');
           dOut.writeChars(tmp.name);
           dOut.writeChar('\n');
       }
  
        
       //如果想要利用ObjectIputStream反串行化构造对象,就必须保证源文件已经是 利用ObjectOutputStream 写入的,否则出现错误
       ObjectOutputStream ObjOut = new ObjectOutputStream(new FileOutputStream(new File("SortWords.txt")));
       for(int i=0; i<index; ++i)
          ObjOut.writeObject(myArray[i]);
       //通过这种方法,可以避免ObjcetInputStream.readObject()中产生EOFException异常
       //ObjOut.writeObject(null);
 
       index=0;
       myTest inputTmp;
       ObjectInputStream ObjIn = new ObjectInputStream(new FileInputStream(new File("SortWords.txt")));
       try{
         while((inputTmp=(myTest)ObjIn.readObject())!=null){
            myArray[index++]=inputTmp;
         }
       }catch(IOException e){e.printStackTrace(); }finally///放生的EOFException异常时IOException的子类
         System.out.println("EOFException处理完毕!");
       }
       System.out.println("Finish!");
   }
}
 
class myTest implements Comparable<myTest>, Serializable{
    int x;
    String name;
    public myTest(){}
    public myTest(int x, String name){
        this.x=x;
        this.name=name;
    }
     
    public int compareTo(myTest tmp){
        if(x==tmp.x)
           return name.compareTo(tmp.name);
        else return x-tmp.x;
    }
 
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
          //in.defaultReadObject();
          x=(int)in.readInt();
          name=(String)in.readObject();
    }
    private void writeObject(ObjectOutputStream out) throws IOException{
          //out.defaultWriteObject();
          out.writeInt(x);
          out.writeObject(name);
    }
}
 
class myComparator implements Comparator<myTest>{
    public int compare(myTest o1, myTest o2){
       if(o1.x==o2.x)
     return o1.name.compareTo(o2.name);
       else return o1.x - o2.x;
    }
     
    public boolean equals(Object o1){
        return true;
    }
}

  










本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/3817782.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值