我有一些类,我正在尝试填充此类的对象。这是我尝试过的。 (问题在下面)
public class Team
{
private String clubName;
private String preName;
private ArrayList<String> branches;
public Team(String clubName, String preName)
{
this.clubName = clubName;
this.preName = preName;
branches = new ArrayList<String>();
}
public Team() {
// TODO Auto-generated constructor stub
}
public String getClubName() { return clubName; }
public String getPreName() { return preName; }
public ArrayList<String> getBranches() { return branches; }
public void setClubName(String clubName) { this.clubName = clubName; }
public void setPreName(String preName) { this.preName = preName; }
public void setBranches(ArrayList<String> branches) { this.branches = branches; }
}
public class Branch
{
private ArrayList<Player> players = new ArrayList<Player>();
String brName;
public Branch() {}
public void setBr(String brName){this.brName = brName;}
public String getBr(){return brName;}
public ArrayList<Player> getPlayers() { return players; }
public void setPlayers(ArrayList<Player> players) { this.players = players; }
}
//测试类
public class test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String a,b,c;
String q = "q";
int brCount = 0, tCount = 0;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
Team[] teams = new Team[30];
Branch[] myBranch = new Branch[30];
for(int z = 0 ; z <30 ;z++)
{
teams[z] = new Team();
myBranch[z] = new Branch();
}
ArrayList<String> tmp = new ArrayList<String>();
int k = 0;
int secim = Integer.parseInt(input.readLine());
while(secim != 0)
{
if(k!=0)
secim = Integer.parseInt(input.readLine());
k++;
switch(secim)
{
case 1 :
brCount = 0;
a = input.readLine();
teams[tCount].setClubName(a);
b= input.readLine();
teams[tCount].setPreName(b);
c = input.readLine();
while(c.equals(q) == false)
{
if(brCount != 0)
{c = input.readLine();}
if(c.equals(q)== false){
myBranch[brCount].brName = c;
tmp.add(myBranch[brCount].brName);
brCount++;
}
System.out.println(brCount);
}
teams[tCount].setBranches(tmp);
for(int i=0;i<=tCount;i++ ){
System.out.print("a :" + teams[i].getClubName()+ " " + teams[i].getPreName()+ " ");
System.out.println(teams[i].getBranches());}
tCount++;
break;
case 2:
String src = input.readLine();//LATERRRRRRRr
}
}
}
}
问题是我的类(class)元素之一。我有一个数组列表作为类的元素。 当我输入:
AAA as preName
BBB as clubName
c
d
e as Branches
然后作为第二个元素
www as preName
GGG as clubName
a
b as branches
The result is coming like:
AAA BBB c,d,e,a,b
GGG www c,d,e,a,b
这意味着 ArrayList 类的一部分正在不断地添加它。我尝试使用clear()方法但引起了问题。任何想法。
最佳答案
问题是两个Team
对象共享对单个 ArrayList<String>
的相同引用。有很多方法可以解决这个问题,但一种方法是让Team
自己管理List<Branch>
,并且它应该只公开 add(Branch)
而不是setBranches(List<Branch>)
。这将对客户端隐藏大部分信息,仅公开最基本的功能,这是一件好事。
另请注意,我使用接口(interface) List<Branch>
而不是ArrayList<Branch>
(或ArrayList<String>
)。这符合Effective Java 第二版,第 52 条:通过接口(interface)引用对象。
我还建议使用 java.util.Scanner
对于 I/O。查看 API 的示例,stackoverflow 上也有很多关于它的问题。这将使代码变得更加简单。
关于java - JAVA中Class内容排列问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3005194/