Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: toString on ArrayList is not working :(

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default toString on ArrayList is not working :(

    I've tried to for() my ArrayList with @Override toString in my Tournament Class and trying to make a simple SOUT in another Class .get(0). Unfortunately the toString doesn't work and I'm just getting the memory position. Any thoughts on where I'm wrong?

    @Override
        public String toString() {
            String teams = "+";
            for(Team t : getTeams()) {
                teams += t.toString();
            }
            return teams;

  2. #2
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: toString on ArrayList is not working :(

    package tournament;
     
    import java.util.ArrayList;
     
    public class Team {
     
        private String name;
        private ArrayList<Player> players;
     
        public Team(String name) {
            this.name = name;
            this.players = new ArrayList<Player>();
        }
     
        public String getName() {
            return name;
        }
     
        public ArrayList<Player> getPlayers() {
            return players;
        }
        @Override
        public String toString() {
            String str = getName() + ":\n";
            for(int i = 0; i < players.size(); i++) {
                str += (i+1) + players.get(i).getName() + "\n";
            }
            return str;
        }
    }
    package tournament;
     
    import java.util.ArrayList;
     
    public class Tournament {
     
        private ArrayList<Team> teams;
     
        public Tournament() {
            this.teams = new ArrayList<Team>();
        }
        public ArrayList<Team> getTeams() {
            return teams;
        }
        @Override
        public String toString() {
            String str = "";
            for(Team t: getTeams()) {
                str += "Team " + t.toString()+"\n";
            }
            return str;
        }
    }

Similar Threads

  1. [SOLVED] ToString
    By Masic1990 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 25th, 2014, 08:40 AM
  2. Array toString
    By smithmar in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 23rd, 2012, 06:10 PM
  3. Can I use toString() here?
    By titowinky in forum Java Theory & Questions
    Replies: 2
    Last Post: May 30th, 2012, 07:15 AM
  4. Can someone help me understand toString()?
    By Psychotron in forum Java Theory & Questions
    Replies: 10
    Last Post: January 18th, 2012, 10:43 PM
  5. [SOLVED] toString() method
    By chronoz13 in forum Object Oriented Programming
    Replies: 12
    Last Post: January 19th, 2010, 06:44 AM

Tags for this Thread