本文介绍了如何在Java中的字符串数组中计算和打印重复的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我手上的困境。经过多次试验和错误,我仍然无法弄清楚这个简单的任务。
I have a dilemma on my hands. After much trial and error, I still could not figure out this simple task.
我有一个数组
String [] array = {anps, anps, anps, bbo, ehllo};
我需要能够遍历数组并找到重复项并在同一行上打印它们。没有重复的单词应该单独显示
I need to be able to go through the array and find duplicates and print them on the same line. Words with no duplicates should be displayed alone
输出必须像这样
anps anps anps
bbo
ehllo
我试过了,for循环,但逻辑似乎不可能。
I have tried while, for loops but the logic seems impossible.
推荐答案
先排序数组,然后
for(int i = 0, i < array.length; i++){
String temp = array[i];
System.out.print(temp+" ");
for(int j = i+1; j < array.length; j++){
String temp2 = array[j];
if(temp.compareTo(temp2) == 0){
System.out.print(temp2+" ");
i++;
}
}
System.out.println();
}
或类似的东西......
or something similar...
这篇关于如何在Java中的字符串数组中计算和打印重复的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!