填充二维数组

发布于 2024-10-21 10:37:10 字数 412 浏览 3 评论 0原文

我有一个包含三个人数据的文本文件。每个人的属性以逗号分隔。它看起来像这样:

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

我最终想要完成的任务是按姓氏升序对这些人进行排序。

不确定我是否以正确的方式思考这个问题,但我想创建一个二维数组,将每个属性分配给正确的行和列。我们现在可以忘记排序。

如何让每一行填充数组的一行,每个单独的属性都在其自己的列中?

也许我应该只是逐行读取文件,然后使用 lineFromFile.charAt(index) 来填充或类似的东西将每个字符添加到二维数组中?任何示例建议将不胜感激。

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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕花眠 2024-10-28 10:37:10

String.split() 在这里可以提供帮助:

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

但是您可能真正想要的是放弃二维数组的想法并创建一个人员列表,其中 Person 是您定义的类。

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], /*...*/));

二维数组几乎总是表明您需要一个类来表示数据“行”。

排序也变得非常容易:

//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);
    }
});

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);
    }
});
并安 2024-10-28 10:37:10

我建议不要使用二维数组并使用您提到的字段创建一个类(根据@Mark的帖子)。然后创建该类实例的一维数组。

但是,如果您想走二维数组路线:

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"}
};

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"}
};
來不及說愛妳 2024-10-28 10:37:10

假设您可以获得一个读取器,

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");

您可以使用缓冲读取器逐行读取它,并使用列表来累积行,

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

然后您可以将列表转换为二维数组。

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

然后您可以使用自定义比较器对其进行排序。

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

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].
  }
})();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文