gpt4 book ai didi

java - 当我尝试添加到树集中时,它返回空白

转载 作者:行者123 更新时间:2023-12-01 14:05:35 25 4
gpt4 key购买 nike

我有一份学校作业,这是我迄今为止所做的,其中记录了我正在尝试做的事情

import java.io.*;
import java.util.Scanner;

public class UniquesDupesTester
{
public static void main( String args[] ) throws IOException
{
// make a Scanner and associate it with "UniquesDupes.dat"
// as long as there are Strings in the file

// read in a String,
// create a UniquesDupes object with it
// print the object

Scanner in = new Scanner(new File("UniquesDupes.dat"));



while (in.hasNextLine())
{
String n = in.nextLine();
UniquesDupes a = new UniquesDupes(n);
a.getUniques();
a.getDupes();
System.out.println (a);
}




}
}

单独的文件

import java.util.Set;
import java.util.TreeSet;
import java.util.Arrays;
import java.util.ArrayList;

public class UniquesDupes
{
private ArrayList<String> list;


/**
* constructs a UniquesDupes object such that list contains the space delimited strings
* parsed from input
* @param input a String containing the list of words separated by spaces
*/
public UniquesDupes(String input)
{
list = new ArrayList<String>();

String[] words = "abc cde fgh ijk".split(" ");
ArrayList<String> list = new ArrayList<String>(Arrays.asList(words));
}

/**
* returns a set of Strings containing each unique entry in the list
*/
public Set<String> getUniques()
{
Set<String> uniques = new TreeSet<String>();

for(String a:list)
{
uniques.add(a);
}

return uniques;
}

/**
* returns a set of Strings containing each entry in the list that occurs more than once
*/
public Set<String> getDupes()
{
Set<String> uniques = new TreeSet<String>();
Set<String> dupes = new TreeSet<String>();

for(String a:list)
{
uniques.add(a);
{
if(uniques.add(a) == false)
{
dupes.add(a);
}
}
}


return dupes;
}

/**
* returns the original list, the list of duplicates and the list of uniques
* @return the String version of the object
*/
public String toString()
{
return "Orig list :: " + list
+ "\nDuplicates :: " + getDupes()
+ "\nUniques :: " + getUniques() + "\n\n";
}
}

如果您需要的话,这是 dat 文件

a b c d e f g h a b c d e f g h i j k
one two three one two three six seven one two
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 6

它编译并运行,但所有文件都返回空白,我不知道我做错了什么,希望得到帮助或提示

最佳答案

逻辑几乎正确。

UniquesDupes 类几乎没问题,但构造函数却不太好。应该只是这样

public UniquesDupes() // no args, default constructor
{
list = new ArrayList<String>(); //just initialize the list
}

同时,该类需要一个 addString 方法:

public void addString(String input) {
String[] words = input.trim().split(" "); //split the input string into words by spaces, after trimming whitespace
this.list.addAll(Arrays.asList(words)); //adding them to the list.
}

while 循环应该稍微改变一下。您只需要 UniquesDupes 类的 1 个实例,然后使用之前创建的 addString 方法添加每一行。

       UniquesDupes a = new UniquesDupes(); //create it here
while (in.hasNextLine())
{
String n = in.nextLine();
a.addString(n); //adding the string
}

那么结果需要以不同的方式处理

            Collection<String> uniques = a.getUniques();
Collection<String> dupes = a.getDupes();

System.out.println (uniques.toString());
System.out.println (dupes.toString());

也就是说,逻辑几乎是正确的......

您所做的一件丑陋的事情是这部分:

    list = new ArrayList<String>(); //using instance variable

String[] words = "abc cde fgh ijk".split(" ");
ArrayList<String> list = new ArrayList<String>(Arrays.asList(words));
// ^^ declaring local variable the same name as the instance variable

不好。你不应该这样做。不,不。不要再这样做了!养成这种习惯会让代码变得异常难以阅读,并且维护起来非常疯狂......

关于java - 当我尝试添加到树集中时,它返回空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18937308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com