简体   繁体   English

不使用 Array.sort 对多维字符串数组进行排序

[英]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()

  • Declared as a public static method, it should take a two-dimensional String array as a parameter, and an integer value for the number of contacts in the array声明为公共静态方法,应以二维String数组为参数,数组中联系人个数为整数值
  • Returns nothing o Sorts the contents of the contact list given as a parameter using the lastName as the sorting field不返回任何内容 o 使用 lastName 作为排序字段对作为参数给出的联系人列表的内容进行排序
  • You may use any of the sorting mechanisms described in the text (bubble, insertion or selection)您可以使用文本中描述的任何排序机制(气泡、插入或选择)
  • Key concept: as you implement the sort, the trick is to identify the dimension for the values you are comparing and how they relate to the values from your loops.关键概念:在实现排序时,诀窍是确定要比较的值的维度以及它们与循环中的值的关系。
  • Hints:提示:
  • Do not rely on the array's length for your loops, instead use the value from the number of contacts不要依赖数组的循环长度,而是使用联系人数量中的值
  • Your temp variable will be an array of size 3, String[] temp = new String[3];您的临时变量将是一个大小为 3 的数组, String[] temp = new String[3];
  • You will need to use the 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-examplehttps://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.

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