[英]Sort Multidimensional String Array Without Using Array.sort
I am new to Java, but to expand my knowledge of it.我是 Java 的新手,但为了扩展我的知识。 Below is a class assignment that I'm having a very, very difficult time completing.下面是我很难完成的课堂作业。 I need to create a method that passes in a two dimensional string array and then sorts the array by the second element.我需要创建一个方法,该方法传入一个二维字符串数组,然后按第二个元素对数组进行排序。
Method sortContacts()方法 sortContacts()
String[] temp = new String[3];
您的临时变量将是一个大小为 3 的数组, String[] temp = new String[3];
string1.compareTo(string2)
method to compare two strings.您将需要使用string1.compareTo(string2)
方法来比较两个字符串。Sample data: (first name, last name, phone number)示例数据:(名字、姓氏、电话号码)
String [][] contactsArray = {
{"Emily","Watson","913-555-0001"},
{"Madison","Jacobs","913-555-0002"},
{"Joshua","Cooper","913-555-0003"},
{"Brandon","Alexander","913-555-0004"},
{"Emma","Miller","913-555-0005"},
{"Daniel","Ward","913-555-0006"},
{"Olivia","Davis","913-555-0007"},
{"Isaac","Torres","913-555-0008"},
{"Austin","Morris","913-555-0009"}
public static void sortContact(String[][] contactsArray, int numContacts) {
String[] temp = new String[3];
String [] words = contactsArray[3];
for(int i = 0; i < numContacts-1; i++)
{
int smallest = i;
for(int j = i + 1; j < numContacts-1; j++)
{
//Prints loop variables so I can see them before any action
System.out.println("First: "+words[j]+" " +"Second "+words[i]);
if(words[j].compareTo(words[i]) < 0)
smallest = j;
}
//put the new minimum in the i-th position.
//String temp = words[i];
words[i] = words[smallest];
words[smallest] = temp[i];
}
}
There are tons of examples for integer arrays but not a lot for string arrays,整数数组的例子有很多,但字符串数组的例子并不多,
Any suggestions are appreciated.任何建议表示赞赏。
I would suggest you go through the OOPs concept and how to implement them in Java.我建议您阅读 OOP 概念以及如何在 Java 中实现它们。 There are hundreds of books, articles and tutorials online for the same, like https://www.journaldev.com/12496/oops-concepts-java-example , https://beginnersbook.com/2013/04/oops-concepts/ , etc.有数以百计的书籍,文章和教程在线一样,像https://www.journaldev.com/12496/oops-concepts-java-example , https://beginnersbook.com/2013/04/oops-concepts /等
As for the answer, you should create a class that extends Comparable
interface to represent each of the rows in the 2D array.至于答案,您应该创建一个扩展Comparable
接口的类来表示二维数组中的每一行。 For example, create a class called Contact
that implements Comparable
interface.例如,创建一个名为Contact
的类,它实现了Comparable
接口。 Implement the compareTo
method from Comparable interface.从 Comparable 接口实现compareTo
方法。 Then create a driver class that creates List of Person and call Collections.sort(list).然后创建一个创建人员列表的驱动程序类并调用 Collections.sort(list)。
I have intentionally left the implementations to you as this is your assignment so that you learn by doing instead of somebody else doing them for you.我有意将实现留给你,因为这是你的任务,这样你就可以边做边学,而不是别人为你做。
Please refer the following links for Comparable interface.有关 Comparable 接口,请参阅以下链接。 https://www.geeksforgeeks.org/comparable-vs-comparator-in-java/ https://www.geeksforgeeks.org/comparable-vs-comparator-in-java/
You can do like this,:你可以这样做:
for(int i=0;i<numContacts;i++) {
for(int j=i+1;j<numContacts;j++) {
if(contactsArray [i][1].compareTo(contactsArray [j][1])>0) {
String[] temp = contactsArray [i];
contactsArray [i] = contactsArray [j];
contactsArray [j] = temp;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.