好的,我是这个网站的新手,但我知道我不应该在这里问HW问题,但我要问的确实是HW,但它已经完成并提交(和评分),我只是希望让我的程序运行得更好理解:)
为了证明我已经完成了它而不是试图快速完成它,这里是提交页面的链接: http://i959.photobucket.com/albums/ae76/GoWxGaiA/HWDone_zps8ae79bf7.png
现在在程序上...我应该创建一个程序,检查用户输入中的重复字符串,然后按排序顺序输出所有“唯一”字符串(不重复的字符串),以及然后在先前输出的正下方按排序顺序输出'非唯一'。我的导师告诉我们,我们必须使用'三重嵌套循环',我假设它只是for循环内for循环的for循环...我得到了所有我需要存储字符串的点一个数组,在这种情况下,我不能,也没有找到另一种方法。我为此作业提交的内容是:
package Homework2;
import java.util.Scanner;
public class Homework2 {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a list of words:");
String[] words = stdin.nextLine().split(" ");
System.out.println(words.length);
String[] array = new String[words.length];
for (int i = 0; i < words.length; i++) {
if(words == words)
for (int j = 0; j < words.length; j++) {
array[j] = words;
System.out.println(array[j]);
}
}
}
}
同样,我想强调一下,我已经提交了这项任务,我只是想正确完成这项任务,以进一步理解。
非常感谢任何帮助!
答案 0 :(得分:0)
此代码存在两个明显的编译时问题:
words == words
永远是真的。我想你正试图测试这条线是否有重复,但这不是这样做的方法。
array[j] = words
尝试将String []分配给String变量。
这项任务实际上比初看起来有点困难。最棘手的部分是按排序顺序打印唯一字符串。最实用的解决方案是使用链接列表,这样可以轻松插入列表中间。我确实假设不允许使用Collections.sort()。我想有可能也不允许使用LinkedList,但你没有在你的问题中提到它。
String[] words = { "1", "4", "2", "4", "3", "1" };
LinkedList<String> unique = new LinkedList<String>();
LinkedList<String> duplicates = new LinkedList<String>();
unique.add(words[0]);
for (int i = 1; i < words.length; i++)
{
boolean found = false;
for (String uniqueWord : unique)
{
if (words[i].equals(uniqueWord))
{
duplicates.add(words[i]);
found = true;
break;
}
}
if (!found)
{
boolean added = false;
for (int index = 0; index < unique.size(); index++)
{
if (words[i].compareTo(unique.get(index)) < 0)
{
unique.add(index, words[i]);
added = true;
break;
}
}
if (!added)
unique.addLast(words[i]);
}
}
for (String word : unique)
System.out.println(word);
System.out.println("--");
for (String word : duplicates)
System.out.println(word);
答案 1 :(得分:0)
我喜欢用于此问题的一个相当简单的解决方案:
Set<String> used = new HashSet<>();
public boolean wasUsed(String s) {
return used.add(s);
}
//using the method
if (wasUsed(yourString)) {
// duplicate!
}
集只包含一个元素,并在添加重复时返回false。使管理变得非常容易。
至于您的方案,我只会单独保留Set
个单词,并在您复制时删除它们。
您可以在Java's Collections上阅读更多内容了解这些有用的工具。
答案 2 :(得分:-1)
以下是一种查找唯一且重复的字符串的方法:
import java.io.*;
public class Test {
public static void main(String[] args){
String[] uniqueStrings = new String[0];
String[] duplicateStrings = new String[0];
BufferedInputStream input = new BufferedInputStream(System.in);
System.out.println("Type Break when you are finished entering strings");
String line = "";
try{
while (true){
int b = input.read();//reads 1 byte of data
line+=(char) b;//turns that byte into a character and adds it to line
if (line.endsWith(System.lineSeparator())){//checks to see if enter has been pressed by checking if line ends with the line separator
line = line.substring(0, line.indexOf(System.lineSeparator()));//cuts the line separator out of the string
if (line.equalsIgnoreCase("Break")){//breaks out of the while loop if break is typed
break;
}
boolean unique = true;//the next 2 for loops check if line is already in duplicate array or unique array and if it is it sets unique to false
for (int i=0;i<uniqueStrings.length;i++){
if (line.equals(uniqueStrings[i])){
unique = false;
}
}
for (int i=0;i<duplicateStrings.length;i++){
if (line.equals(duplicateStrings[i])){
unique = false;
}
}
if (unique){
String[] array = new String[uniqueStrings.length+1];
for (int i=0;i<uniqueStrings.length;i++){
array[i] = uniqueStrings[i];
}
array[array.length-1] = line;
uniqueStrings = array;//adds the line into the unique array
}else{
boolean alreadyAdded = false;//checks if line is already in duplicate array
for (int i=0;i<duplicateStrings.length;i++){
if (line.equals(duplicateStrings[i])){
alreadyAdded = true;
}
}
if (!alreadyAdded){
String[] array = new String[duplicateStrings.length+1];
for (int i=0;i<duplicateStrings.length;i++){
array[i] = duplicateStrings[i];
}
array[array.length-1] = line;
duplicateStrings = array;//adds line to the duplicate array
String[] array2 = new String[uniqueStrings.length-1];
boolean removed = false;
for (int i=0;i<uniqueStrings.length;i++){
if (uniqueStrings[i].equals(line)){
removed = true;
}else{
if (removed){
array2[i-1] = uniqueStrings[i];
}else{
array2[i] = uniqueStrings[i];
}
}
}
uniqueStrings = array2;//removes line from unique array
}
}
line = "";//resets the line variable so the next line can be read
}
}
System.out.println(System.lineSeparator()+"Unique Strings:");
for (int i=0;i<uniqueStrings.length;i++){//prints out unique strings
System.out.println(uniqueStrings[i]);
}
System.out.println(System.lineSeparator()+"Duplicate Strings:");
for (int i=0;i<duplicateStrings.length;i++){//prints out duplicate strings
System.out.println(duplicateStrings[i]);
}
}catch(Exception error){error.printStackTrace();}
System.exit(-1);
}
}