Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Collections Sort可对两个ArrayList进行相同排序_Java_Sorting_Arraylist_Collections - Fatal编程技术网

Java Collections Sort可对两个ArrayList进行相同排序

Java Collections Sort可对两个ArrayList进行相同排序,java,sorting,arraylist,collections,Java,Sorting,Arraylist,Collections,我的程序必须使用Collections sort方法按字典顺序对字符串的ArrayList进行排序,但每个字符串都有一个对应的整数值存储在单独的ArrayList中。我想对它们进行相同的排序,以便整数值保留在正确的字符串中。如果你知道存储这两个值的更好方法,我洗耳恭听 public class a5p1b { public static void main(String[] args) { Scanner input = new Scanner(System.in).u

我的程序必须使用Collections sort方法按字典顺序对字符串的ArrayList进行排序,但每个字符串都有一个对应的整数值存储在单独的ArrayList中。我想对它们进行相同的排序,以便整数值保留在正确的字符串中。如果你知道存储这两个值的更好方法,我洗耳恭听

public class a5p1b {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in).useDelimiter("[^a-zA-z]+");
        // ArrayLists to store the Strings and the frequencies
        ArrayList<String> lst = new ArrayList<String>();
        ArrayList<Integer> intLst = new ArrayList<Integer>();

        //loops through as long as there is user input
        while (input.hasNext()) {
            String str = input.next().toLowerCase();
            // if the list already has the string it doesn't add it and it
            // ups the count by 1
            if (lst.contains(str)) {
                int index = lst.indexOf(str);
                intLst.set(index, intLst.get(index) + 1);
            } else {
                // if the word hasnt been found yet it adds it to the list
                lst.add(str);
                intLst.add(1);
            }
        }
    }    
}
公共级a5p1b{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in)。使用分隔符(“[^a-zA-z]+”);
//ArrayList用于存储字符串和频率
ArrayList lst=新的ArrayList();
ArrayList intLst=新的ArrayList();
//只要有用户输入,就可以循环
while(input.hasNext()){
String str=input.next().toLowerCase();
//如果列表中已经有字符串,则不会添加该字符串,并且
//把计数提高1
如果(lst.包含(str)){
int index=lst.indexOf(str);
intLst.set(索引,intLst.get(索引)+1);
}否则{
//如果尚未找到该单词,则会将其添加到列表中
第一次添加(str);
补充(1);
}
}
}    
}

您的抽象理解有误。如果该字符串和该数字属于同一类,则不要将它们保存在两个不同的列表中

而是创建一个包含这两个值的类(或者使用现有的类之一)。然后可以为该类提供一个equals方法;加上一个特定的,只比较字符串元素的

最后,将该类的对象放入一个列表中;然后对列表进行排序

好的OO编程的全部思想是创建有用的抽象

作为记录:正如dnault所建议的,如果字符串和数字之间确实没有“紧密”耦合,那么您也可以使用(用作
树映射)来对带有数字的字符串进行排序。

试试看

inList.sort(Comparator.comparing(i -> i.toString());

尽管如此,我认为这两个列表不是一个好主意。

您应该使用映射将每个唯一的字符串键与一个整数值关联起来

然后可以调用由
keySet()
返回的映射键集上的Collections.sort


此外,如果使用诸如TreeMap之类的SortedMap,则无需对键进行排序。但是,该解决方案可能无法满足“作业5问题1b”的要求。

您希望它们按数字或字典顺序排序吗?但也许你可以将它们存储在一个映射中。使用一个从字符串到整数的映射,然后对键进行排序,然后按排序的顺序取出值?树形映射也可能是一个可行的选择。@d诺尔特我在遛狗时也有同样的想法;但是谢谢你的投入;我相应地更新了我的答案。