简体   繁体   English

填充二维数组

[英]populating a 2D array

I've got a text file with dats for three people. 我有一个带人的文本文件,可供三个人使用。 The properties for each person is separated by a comma. 每个人的属性都用逗号分隔。 It looks like this: 看起来像这样:

Patrick, Gary, Male, Blue, 1/28/1948
Carson, Larry, Male, Pink, 11/24/1976
Fisher, Paul, Male, Orange, 5/12/1995

What I'm trying to accomplish ultimately to to sort those people by their last name in ascending order. 我最终要完成的工作是按照姓氏的升序对这些人进行排序。

Not sure I'm thinking about this the right way, but I wanted to create a 2D array that would assign each property to the proper row and column. 不确定我是否在考虑正确的方法,但是我想创建一个2D数组,将每个属性分配给正确的行和列。 We can forget about the sorting for now. 现在我们可以忘记排序了。

How can I get each line to populate a row of the array, with each separate property in its own column? 如何获得每一行来填充数组的一行,并在其自己的列中包含每个单独的属性?

Maybe I should just be reading the file line by line, then adding each character to a 2D array using lineFromFile.charAt(index) to populate or something like that? 也许我应该逐行读取文件,然后使用lineFromFile.charAt(index)将每个字符添加到2D数组中以进行填充或类似的操作? Any sample suggestions would be greatly appreciated. 任何样品建议将不胜感激。

String.split() can be of help here: String.split()在这里可以有所帮助:

person[i] = line.split(", ");

But what you probably really want is to abandon the idea of a 2-D array and create a List of Persons, where Person is a class that you define. 但是,您可能真正想要的是放弃二维数组的想法,而是创建一个“人的列表”,其中“人”是您定义的类。

public class Person {
  public final String firstName;
  public final String lastName;
  public final String birthDate; //should really be a java.util.Date
  //...plus a constructor for the above
}

//...
List<Person> people = new ArrayList<Person>();
String line = reader.readLine();
String[] fields = line.split(", ");
people.add(new Person(fields[0], fields[1], /*...*/));

A 2D array almost always suggests that you need a class to represent a "row" of data. 一个2D数组几乎总是建议您需要一个类来表示“行”数据。

Sorting becomes very easy too: 排序也变得非常容易:

//sorts the people in descending order by first name
people.sort(new Comparator<Person>() {
    public int compare(Person a, Person b) {
       b.firstName.compareTo(a.firstName);
    }
});

I would suggest not using a 2D array and creating a class with the fields you mention (per @Mark's post). 我建议不要使用2D数组,使用您提到的字段创建类(每个@Mark的文章)。 Then creating a 1D array of instances of that class. 然后创建该类实例的一维数组。

However, if you want to go the 2D array route: 但是,如果要走二维阵列路线:

String[][] people = new String[][] {
  {"Patrick", "Gary", "Male", "Blue", "1/28/1948"},
  {"Carson", "Larry", "Male", "Pink", "11/24/1976"},
  {"Fisher", "Paul", "Male", "Orange", "5/12/1995"}
};

Assuming you can get a reader 假设你可以得到一个读者

StringReader in = new StringReader(
    "Patrick, Gary, Male, Blue, 1/28/1948\n" +
    "Carson, Larry, Male, Pink, 11/24/1976\n" +
    "Fisher, Paul, Male, Orange, 5/12/1995");

you can read it line by line using a buffered reader and use a list to accumulate the lines 您可以使用缓冲读取器逐行读取它,并使用列表来累积行

List<String[]> splitLines = new ArrayList<String[]>();
try {
  BufferedReader lineIn = new BufferedReader(in);
  for (String line; (line = lineIn.readLine()) != null;) {
    String[] lineParts = line.split(",");
    // Maybe check that lineParts has the right length here.
    splitLines.add(lineParts);
  }
} finally {
  in.close();
}

then you can convert the list to a 2-D array. 然后您可以将列表转换为二维数组。

String[][] linesArray = splitLines.toArray(new String[][] {});

which you can then sort using a custom comparator. 然后您可以使用自定义比较器对其进行排序。

Array.sort(linesArray, new Comparator<String[]>() {
  public int compare(String[] linea, String[] lineb) {
    ...  // the date should be in linea[4] and lineb[4].
  }
})();

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

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