开发者

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 a开发者_JAVA百科scending 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. 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? Any sample suggestions would be greatly appreciated.


String.split() can be of help here:

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.

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). 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].
  }
})();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜