Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 856001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:10:11+00:00 2026-05-15T08:10:11+00:00

I have some classes and I’m trying to fill the objects of this class.

  • 0

I have some classes and I’m trying to fill the objects of this class. Here is what i’ve tried. (Question is at the below)

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; }
}

//TEST CLASS

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

    }

    }
}

}

The problem is one of my class elements. I have an arraylist as an element of a class.
When i enter:

AAA as preName
BBB as clubName
c
d
e   as Branches

Then as a second element

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

Which means ArrayList part of the class is putting it on and on. I tried to use clear() method but caused problems. Any ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:10:11+00:00Added an answer on May 15, 2026 at 8:10 am

    You need to copy lists in setters, otherwise you are using the same list (tmp) everywhere, so no wonder it has the same contents:

    public void setBranches(List<String> branches) { 
        this.branches = new ArrayList<String>(branches); 
    }
    public void setPlayers(List<Player> players) { 
        this.players = new ArrayList<Player>(players); 
    }
    

    Theoretically, you also need to copy or wrap it in getters, but that’s another story.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some classes layed out like this class A { public virtual void
I have some classes extended this way: class Baseresidence extends CActiveRecord { public static
I have some classes that look like this: MODEL public abstract class BaseEntity<O extends
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
I have some classes like this: interface class IA { }; interface class IB
Say that i have some classes like this example. class A { int k,
I have some classes that represent immutable objects (Quantity, Price, Probability). Is there some
I have some classes as below, i have implemented the Equals(Object) method for almost
I have some classes(call it Class A) which I would like to unit test
Say I have an interface and some classes: public interface IPanel<ComponentType extends Component> {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.