在methodB中,我做了一个for循环,其中不显示重复的名称。如果我在methodASet中这样做for循环,它将起作用。当我尝试在methodB中执行此操作时,出现错误消息“找不到符号-变量名1”。通常我使用this.nameofthemethodheader();,但现在我知道我无法对TreeSet执行此操作。有人可以在这里帮助我吗?这是代码。谢谢。请记住,我想使用此for循环完成。
public class MyMates
{
public MyMates()
{
super();
names = new TreeSet<String>();
}
public static void methodASet()
{
String[] name1 = new String[] {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
String[] name2 = new String[] { "Alan", "Amy", "Jeremy", "Helen", "Alexi"};
String[] name3 = new String[] { "Adel", "Aaron", "Amy", "James", "Alice" };
}
public static void methodB()
{
for (int i = 0; i < name1.length; i++)
{
names.add(name1[i]);
}
System.out.println(names);
for (int i = 0; i < name2.length; i++)
{
names.add(name2[i]);
}
System.out.println(names);
for (int i = 0; i < name3.length; i++)
{
names.add(name3[i]);
}
System.out.println(names);
Dialog.alert("repeated names not selected");
}
最佳答案
好的,尝试一下:(我写它只是为了工作……在我看来,这是一个糟糕的代码)
import java.util.Set;
import java.util.TreeSet;
public class MyMates {
private static String[] name1 = null;
private static String[] name2 = null;
private static String[] name3 = null;
private static Set<String> names;
public MyMates() {
methodASet();
names = new TreeSet<String>();
}
public static void methodASet() {
name1 = new String[]{"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
name2 = new String[]{"Alan", "Amy", "Jeremy", "Helen", "Alexi"};
name3 = new String[]{"Adel", "Aaron", "Amy", "James", "Alice"};
}
public static void methodB() {
for (int i = 0; i < name1.length; i++) {
names.add(name1[i]);
}
System.out.println(names);
for (int i = 0; i < name2.length; i++) {
names.add(name2[i]);
}
System.out.println(names);
for (int i = 0; i < name3.length; i++) {
names.add(name3[i]);
}
System.out.println(names);
}
public static void main(String[] args) {
MyMates polop = new MyMates();
MyMates.methodB();
}
}
打印:
[爱丽丝,艾米,杰里米,何塞,帕特里克]
[艾伦,阿列克西,爱丽丝,艾米,海伦,杰里米,何塞,帕特里克]
[亚伦,阿德尔,艾伦,阿列克谢,爱丽丝,艾米,海伦,詹姆斯,杰里米,何塞,帕特里克]
关于java - 找不到符号-变量名1-TreeSet,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3238907/