【问题标题】:Collections.frequency not working correctly in AndroidCollections.frequency 在 Android 中无法正常工作
【发布时间】:2015-05-28 01:18:56
【问题描述】:

我的 Android 手机中有一个代码,可以通过 collections.frequency 查找重复号码。此代码仅在 android 上的 java 程序中运行良好。但不是作为android上的应用程序。这是我在 android 中的代码。

ArrayList<String> ll = new ArrayList<String>();
String item = inputText.getText().toString();
ll.add(item);
HashSet<String> set = new HashSet<>(ll);
for (String temp : set)
{
    answertext.setText(temp + "shows that many times: " + Collections.frequency(ll, temp));
}

输出如下:

33 44 33 44 shows that many times: 1

如果用户通过文本框输入数字,它不会找到任何重复项。 但是,如果在代码中去掉用户输入并用这个输入替换它:

ll.add("33");
ll.add("44");
ll.add("33");
ll.add("44");
ll.add("24");
ll.add("24");

输出将是这样的:

44 shows that many times: 2

因此,在此输入中,collections.frequency 正在查找重复数字。但为什么只有一个数字?为什么是 44 而不是 33?为什么它不像只在手机上作为 java 程序那样输出所有重复的数字。不涉及安卓? 我想让它与来自文本框的用户输入一起工作。 在运行良好的 Java 端,我得到了以下代码:

List<String> list = new ArrayList<String>();
Scanner stdin = new Scanner(System.in);
System.out.println("Enter the amount of numbers you want to input: Input numbers separated by a space.");
int n = stdin.nextInt();
for (int i = 0; i < n; i++) 
{
    list.add(stdin.next());
}
System.out.println("\nCount all with frequency");
Set<String> uniqueSet = new HashSet<String>(list);
for (String temp : uniqueSet) 
{
    System.out.println(temp + " shows that many times : " + Collections.frequency(list, temp)); 
}
//Enter the amount of numbers you want to input
12 //hit the return key
22 33 44 22 33 44 22 33 44 22 33 44
//the output is like so:
Count all with frequency
33 shows that many times: 4
44 shows that many times: 4
22 shows that many times: 4

为什么这段代码可以在 Java 中运行,但不能在 android 中运行?

【问题讨论】:

  • 在您的 Android 代码中,您的列表中只有一项。如果只有一个项目,它怎么会有重复的项目?
  • 好的。如果它只有一个项目,用户如何添加更多项目?以及为什么使用此输入:ll.add("33"); ll.add("44"); ll.add("33"); ll.add("44"); ll.add("24"); ll.add("24");输出将如下所示: 44 多次显示: 2. 为什么选择数字 44 显示为重复项?为什么不是所有数字?数组中有更多的项,对吧?我怎样才能使它与用户输入一起工作?
  • 嗨 immibis,你是个天才。我添加了第二个文本框作为按钮的输入框。并在一个框中输入数字 44 并在第二个框 44 中输入。现在,当单击按钮时,它会显示: 44 多次显示: 2. 好像你解决了我的问题。根据这个,我需要做的就是在代码中添加更多的文本框。但是如果我想添加 50 个左右的数字怎么办?这将需要屏幕上的大量文本框。您是否知道只有一个框可以输入这么多数字的任何其他方法?这可能吗?
  • 嗨,Immibis,非常感谢您的帮助。该代码 sn-p 成功了。现在,如果我添加中间有空格的数字,它会将每个数字作为一个字符串并将其放入 ArrayList 中。在最后添加 .getText 后,它现在输出如下:44 显示多次:4. 22 显示多次:4. 完美。也不知道输入的 .split 部分。这真的解决了它。再次,非常感谢你。我真的很高兴。特别是因为其他人搁置了最后一个帖子,然后甚至关闭了它。但你没有放弃我。我很感激。谢谢

标签: java android hashset


【解决方案1】:
String item = inputText.getText().toString();
ll.add(item);

这会将一项添加到列表中。我猜你想单独添加每个单词,你可以这样做:

// split the input apart at the spaces
String[] items = inputText.getText().toString().split(" ");

// then add each part separately to the list
for(String item : items)
    ll.add(item);

您的第二个问题是 setText... 设置文本。它不会添加到文本的末尾,它会替换已经存在的内容。我不知道你打算如何显示多个字符串,但你可以这样做:

// clear answertext
answertext.setText("");

for (String temp : set)
{
    // set the text to <whatever was already there> followed by this item
    answertext.setText(answertext.getText() + temp + "shows that many times: " + Collections.frequency(ll, temp) + "\n");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2012-09-13
    • 2012-03-24
    相关资源
    最近更新 更多