有没有一种方法可以对一个Integer数组进行排序并对另一个String数组进行相同的更改

时间:2019-11-13 09:59:37

标签: java arrays

我在一个学校项目上工作,而不是让我编写程序而不是使用String来计算String中所有单词出现的次数。我设法通过制作数组来做到这一点。一个数组是String数组,它包含单词,而另一个Integer数组则包含单词出现的次数。一个特定的单词及其出现的次数在两个数组中具有相同的元素编号。

但是我的问题是按降序对它们进行排序,我最初认为可以通过使用以下代码行来做到这一点:

 Arrays.sort(thenumbers, Collections.reverseOrder()); 

但是我意识到这将对单词出现的次数进行排序,但是这会破坏整个程序,因为单词和数字在数组中具有不同的元素编号。

有没有办法解决这个问题?

任何帮助将不胜感激,谢谢!

4 个答案:

答案 0 :(得分:0)

如果您只想使用数组而不使用Maps,因此带有count的单词在同一对象中不相关,则可以实现一个自定义的bublesort,它根据count数组对两个数组进行排序。

例如:

static void bubbleSort(String[] words, int[] wordCount) {
    int n = wordCount.length;
    String wordTemp = null;
    int countTemp = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (n - i); j++) {
            if (wordCount[j - 1] < wordCount[j]) { // < for reverse order, > for ascend
                //swap words elements
                wordTemp = words[j-1];
                words[j-1] = words[j];
                words[j] = wordTemp;
                //swap wordcount elements
                countTemp = wordCount[j - 1];
                wordCount[j - 1] = wordCount[j];
                wordCount[j] = countTemp;
            }
        }
    }
}

它将对wordCount应用bublesort算法(该算法可保存每个单词的出现,并在wordCount排序的同时对单词进行排序。

一样运行以下示例
String [] words = {"four", "one", "five", "three", "six", "two"};
int [] wordCount = {4, 1, 5, 3, 6, 2};
bubbleSort(words, wordCount);
System.out.println("Words: " + Arrays.toString(words));
System.out.println("Word Count: " + Arrays.toString(wordCount));

生产

Words: [six, five, four, three, two, one]
Word Count: [6, 5, 4, 3, 2, 1]

答案 1 :(得分:0)

您不必制作2个单独的数组。找到每个单词在String中出现多少次具有使用流API的标准解决方案:

import static java.util.stream.Collectors.*;

Map<String, Long> map = Arrays.stream(sourceString.split(" "))
        .collect(groupingBy(Function.identity(), counting()))

这为您提供了一个以 word 为键,并以它出现了多少次作为值的地图。 然后,您可以使用Sort a Map<Key, Value> by values

中的任何解决方案按值对地图进行排序

答案 2 :(得分:0)

独特的非答案:进行真正的OOP设计。

您在这里做的是:您在两个不同的地方放置了数据,并且您手动通过以下方式“映射”了相应的项目:在这两个数组中使用相同的 index

基本上是:过程编程。

OOP答案:创建一个类,该类表示单个单词一起及其出现次数!然后,您可以实现Comparable接口,并一键式排序此类对象的列表!

含义:其他答案在技术上都是正确的,因为它们告诉您在当前设计下如何解决问题。但是真正的答案是:用不同的东西替换您当前的设计,这将使更简单的解决方案!

答案 3 :(得分:0)

我不得不接受this answer的建议,这建议使用OOP设计,该类具有封装字符串和计数的类。

作为一种临时解决方案,您可以对索引数组而不是数组本身进行排序。使用比较器使用索引来提取一个数组的值类似于使用专用类型的属性的比较器。

使用专用对象的不同之处在于,您必须将索引应用于原始数组,以在后续步骤中将它们按所需的顺序放置

String[] strings = { "foo", "bar", "baz" };
int[]    counts  = {     1,     3,     2 };

// create array of ascending indices
Integer[] indices = IntStream.range(0, strings.length).boxed().toArray(Integer[]::new);
// sort by counts, decending
Arrays.sort(indices, Comparator.comparing(ix -> counts[ix], Comparator.reverseOrder()));

// apply to counts
int[] newCounts = Arrays.stream(indices).mapToInt(ix -> counts[ix]).toArray();
// apply to strings
String[] newStrings = Arrays.stream(indices).map(ix -> strings[ix]).toArray(String[]::new);

System.out.println(Arrays.toString(newStrings));
System.out.println(Arrays.toString(newCounts));
[bar, baz, foo]
[3, 2, 1]