0
18 14
19 15
20 16
21 17
22 18
23 19
24 20
25 20
25 21

47 44
48 44
48 45
49 44

49 43
49 42
50 42
50 43
51 43
53 40
53 39
53 38
54 38
54 39

我的工作是我应该读取这个输入文件,删除空行并在这里对两列进行排序我的java代码试图读取文件并打印出它的值:这里我将整数存储在不同的数组中,代码是:

import java.io.*;
import java.lang.*;
import java.util.*;

public class read
 {
        public static void main(String[] args) throws Exception 
        {
        System.out.println("Enter file name");
        DataInputStream dis=new DataInputStream(System.in);
        String dir1=dis.readLine();
        File infile = new File(dir1);
        System.out.println("Enter output file name");
        DataInputStream dis2=new DataInputStream(System.in);
        String dir3=dis.readLine();
        String path="E:/photos";
        String newpath=path + "/" +dir3;
        File outfile = new File(newpath);
        int newcount=0,newcount1=0;
        FileReader fr=new FileReader(infile);
        BufferedReader fr11= new BufferedReader(fr);
        FileWriter fw = new FileWriter(outfile);
        BufferedWriter bufferFileWriter  = new BufferedWriter(fw);
        Scanner input = new Scanner(infile);
        String[] outputArray1 = new String[31];
        String[] outputArray2 = new String[31];
        int i = 0;
               while (input.hasNextLine()) 
                {
                        String line = input.nextLine();
                        if(line.length() > 0)
                {
                        String[] columns = line.split(" ");
                        System.out.println("my first column : "+ columns[0] );
                        System.out.println("my second column : "+ columns[1] );
                        outputArray1[i] = columns[0];
                        outputArray2[i] = columns[1];
                        i++;
                }
                }
                String[][] temp = new String[2][];
                temp[0]= outputArray1;
                temp[1]= outputArray2;
               for (int k=0;k<2;k++)
        for (int j=0;j<i;j++)
        {
System.out.println("new row"+k+"new col"+j+"value="+temp[k][j]);
        }
        if (temp.length > 0) {
            for (int m = 0; m< temp[0].length; m++) {
                for (int n = 0; n< temp.length; n++) {
                    System.out.print(temp[n][m] + " ");
                }
                System.out.print("\n");
            }
        }
                       fr.close();
                       fw.close();
                    bufferFileWriter.close();
}
}
4

3 回答 3

2

您可以采用这种方法,创建一个List<Item>其中 Item 是包含 2 个 Column 值的类型。(x1 和 x2)

然后编写一个compareTo(Item o)比较在比较中呈现给它的两个 Item 对象的 x1 值,如果给出了明确的答案,则返回该答案。

public class Item implements Comparable<Item> {
    private Integer int1;
    private Integer int2;

    @Override
    public int compareTo(Item o) {
        return int1 > (o.int1);
    }
}

希望这可以帮助。

于 2013-09-02T19:08:06.847 回答
0

我的问题的答案是:

import java.io.*;
import java.lang.*;
import java.util.*;

public class readnew
 {
        public static void main(String[] args) throws Exception 
        {
        System.out.println("Enter file name");
        DataInputStream dis=new DataInputStream(System.in);
        String dir1=dis.readLine();
        File infile = new File(dir1);
        System.out.println("Enter output file name");
        DataInputStream dis2=new DataInputStream(System.in);
        String dir3=dis.readLine();
        String path="E:/photos";
        String newpath=path + "/" +dir3;
        File outfile = new File(newpath);
        int newcount=0,newcount1=0;
        FileReader fr=new FileReader(infile);
        BufferedReader fr11= new BufferedReader(fr);
        FileWriter fw = new FileWriter(outfile);
        BufferedWriter bufferFileWriter  = new BufferedWriter(fw);
        Scanner input = new Scanner(infile);
        String[] outputArray1 = new String[31];
        String[] outputArray2 = new String[31];
        int i = 0;
               while (input.hasNextLine()) 
                {
                        String line = input.nextLine();
                        if(line.length() > 0)
                {
                        String[] columns = line.split(" ");
                        System.out.println("my first column : "+ columns[0] );
                        System.out.println("my second column : "+ columns[1] );
                        outputArray1[i] = columns[0];
                        outputArray2[i] = columns[1];
                        i++;
                }
                }
                String[][] temp = new String[2][];
                String[][] temp1 = new String[i][2];
                temp[0]= outputArray1;
                temp[1]= outputArray2;
               for (int k=0;k<2;k++)
        for (int j=0;j<i;j++)
        {
System.out.println("new row"+k+"new col"+j+"value="+temp[k][j]);
        }
        if (temp.length > 0) {
            for (int m = 0; m< temp[0].length; m++) {
                for (int n = 0; n< temp.length; n++) {
                temp1[m][n]=temp[n][m];
                    System.out.print(temp[n][m] + " ");
                }
                System.out.print("\n");
            }
        }
        System.out.println("transpose");
        for (int a=0;a<i;a++)
        for (int b=0;b<2;b++)
        {
        System.out.println("new row"+a+"new col"+b+"value="+temp1[a][b]);
        }
        System.out.println("sort");
    final Comparator<String[]> arrayComparator = new Comparator<String[]>() 
    {
        @Override
        public int compare(String[] o1, String[] o2)
        {
            return o1[1].compareTo(o2[1]);
        }
    };
    final Comparator<String[]> arrayComparator1 = new Comparator<String[]>() 
    {
        @Override
        public int compare(String[] o1, String[] o2) 
        {
            return o1[0].compareTo(o2[0]);
        }
    };
     Arrays.sort(temp1, arrayComparator);
     Arrays.sort(temp1, arrayComparator1);
     for(int p=0; p<i; p++)
        {
            for(int q=0; q<2; q++) 
            {
                System.out.print(temp1[p][q]+" ");
            }
            System.out.print("\n");
        }

                       fr.close();
                       fw.close();
                    bufferFileWriter.close();
}
}

输出是:

18 14
18 14
19 15
19 15
20 16
20 16
21 17
21 17
22 18
22 18
22 18
22 18
23 19
23 19
23 19
23 19
24 20
24 20
24 20
25 20
25 20
25 20
25 21
25 21
25 21
26 21
26 21
26 21
27 21
27 21
27 21
于 2013-09-03T05:32:49.773 回答
0

以下内容可能会让您对您可以做什么有所了解。这个例子使用了 Java 7 的一些特性。排序C1 ASC, C2 DESC类似于 SQL ORDER BY,其中C1第一列是C2,第二列是。

public static class Row {
    public Row(String c1, String c2) {
        this.c1 = c1;
        this.c2 = c2;
    }
    String c1;
    String c2;
}

public static void main(String[] args) throws Exception {
    JFileChooser fileChooser = new JFileChooser();
    int option = fileChooser.showOpenDialog(null);
    if (option == JFileChooser.APPROVE_OPTION) {
        Path inputPath = fileChooser.getSelectedFile().toPath();
        option = fileChooser.showSaveDialog(null);
        if (option == JFileChooser.APPROVE_OPTION) {
            Path outputPath = fileChooser.getSelectedFile().toPath();
            sortAndSave(inputPath, outputPath);
        }
    }
}

public static void sortAndSave(Path inputPath, Path outputPath)
        throws IOException {
    List<String> lines = Files.readAllLines(inputPath,
            StandardCharsets.UTF_8);
    List<Row> rows = new ArrayList<>();
    for (String line : lines) {
        String[] array = line.split("\\s+");
        rows.add(new Row(array[0], array[1]));
    }
    Collections.sort(rows, new Comparator<Row>() {
        public int compare(Row r1, Row r2) {
            // C1 ASC, C2 DESC
            if (r1.c1.compareTo(r2.c1) == 0) {
                return r2.c2.compareTo(r1.c2);
            }
            return r1.c1.compareTo(r2.c1);
        }
    });
    try (BufferedWriter writer = Files.newBufferedWriter(outputPath,
            StandardCharsets.UTF_8);
            PrintWriter out = new PrintWriter(writer);) {
        for (Row row : rows) {
            out.println(row.c1 + " " + row.c2);
        }
        out.flush();
    }
}

sort()如您所见,在调用类中的方法时使用了比较器java.util.Collections

输出:

18 14
19 15
20 16
21 17
22 18
23 19
24 20
25 21
25 20
47 44
48 45
48 44
49 44
49 43
49 42
50 43
50 42
51 43
53 40
53 39
53 38
54 39
54 38
于 2013-09-02T20:11:14.653 回答