简体   繁体   English

使用主键对2D数组排序

[英]Sort a 2D Array with primary key

I'm trying to split each line of a text file and insert into an array list. 我正在尝试拆分文本文件的每一行并插入到数组列表中。

aa 04 cc ff gg rrr
aa 01 bb dd ee fff
aa 03 ff hh ee ttt
aa 05 dd ss ww ccc

1, How to store number as an integer and rest are strings in array list. 1,如何将数字存储为整数,其余的是数组列表中的字符串。 (throwing Number Format Error with current code). (使用当前代码引发数字格式错误)。

2, Is it possible to sort based on the integer while I am inserting to array with Java collections? 2,当我使用Java集合插入数组时,是否可以基于整数排序?

So far, I have, 到目前为止,我已经

List <Object> records = new ArrayList<Object>();
        bf = new BufferedReader(new FileReader(getFile()));
        String readLine;        

        while ((readLine = bf.readLine()) != null) {
            try {
                    List <Object> record = new ArrayList<Object>();

                    record.add(readLine.substring(flg_start, num_start).trim());
                    record.add(Integer.parseInt(readLine.substring(num_start-1, fld_start-1).trim())); // converting string to int and saving to record list, throwing Number Format Error.

let me rephrase my questions again, How can I set record [1] as integer and rest as string? 让我再次表述我的问题:如何将记录[1]设置为整数,如何将其设置为字符串? What collection framework I can use to sort when I am adding into array? 将数组添加到数组时可以使用哪种收集框架进行排序? I am expecting the end result in my array as a sorted list of around 1M records 我期望数组中的最终结果是大约1M条记录的排序列表

aa 01 bb dd ee fff
aa 03 ff hh ee ttt
aa 04 cc ff gg rrr
aa 05 dd ss ww ccc

As for the data structure looks like you need TreeMap with Integer keys and List values. 至于数据结构,则需要带Integer键和List值的TreeMap You should read the file and parse each line as @HaridwarJha described and for each line put new value with the parsed Integer key and List value formed of the parsed values. 您应该阅读文件并按@HaridwarJha所述解析每一行,并为每一行添加新值,其中包含解析后的Integer键和由解析后的值形成的List值。 With the TreeMap your data will be sorted by keys, ie by Integer values from you file. 使用TreeMap您的数据将按键(即文件中的Integer值)进行排序。

Map<Integer, List<Object>> map = new TreeMap<Integer, List<Object>>();
while((line=reader.readLine())!=null){
    List<Object> row = new ArrayList<>();
    String[] strArray=line.split(" ");
    row.add(strArray[0]);
    row.add(strArray[1]);
    row.add(strArray[2]);
    row.add(strArray[3]);
    row.add(strArray[4]);
    row.add(strArray[5]);
    map.put(new Integer(row.get(1)), row);
}

You can do like this and to access strArray you can use for loop or can be use regex[0...1] for parsing.... Here is simple one...try it public static void main(String[] args) { List records = new ArrayList(); 您可以这样做,并访问strArray,可以将其用于循环,也可以使用regex [0 ... 1]进行解析。...这很简单...尝试使用public static void main(String [] args ){列表记录= new ArrayList();

    File file=new File("File Location");
    try {
        BufferedReader reader=new BufferedReader(new FileReader(file));
        String line;
        while((line=reader.readLine())!=null){
            String[] strArray=line.split(" ");
            records.add(new Integer(strArray[1]));
            records.add(new String(strArray[0]));
            records.add(new String(strArray[2]));
            records.add(new String(strArray[3]));
            records.add(new String(strArray[4]));
            records.add(new String(strArray[5]));
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for(Object tmp:records){
        System.out.println(tmp);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM