7 Replies - 710 Views - Last Post: 01 October 2013 - 01:43 AM Rate Topic: -----

#1 mushkyn   User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 42
  • Joined: 28-March 13

Displaying the contents of an Arraylist

Posted 30 September 2013 - 09:14 PM

Warning: Java noob here.

I'm wondering how I can display the contents of my Arraylist, index by index, when each index contains an object full of String data. I followed what I've seen in several tutorials but my program is slightly different being several Strings per object and does not output correctly; it seems like my display method is only displaying the memory reference rather than the actual contents.

My add method looks like:

 public static void addStuff(ArrayList<Stuff> listOfStuff)
    {
        Scanner input=new Scanner(System.in);
        System.out.println("Please enter value A");
        String A = input.nextLine();
        
        System.out.println("Please enter value B.");
        String B = input.nextLine();
        
        System.out.println("Please enter value C.");
        String C = input.nextLine();
        
        Stuff stuff= new Stuff(A, B, C);  

        listOfStuff.add(stuff);
        }




and the display method that only outputs hexidecimal gobbledygook:

public static void displayStudents(ArrayList<Stuff> listOfStuff)
    {
        for (int i=0; i<listOfStuff.size(); i++)
        {
            System.out.println(listOfStuff.get(i)); 
            // Seems to be printing memory reference instead of String data,
            //how do I change this to show the contents of stuff A B C?
        }
    }



This post has been edited by mushkyn: 30 September 2013 - 09:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Displaying the contents of an Arraylist

#2 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: Displaying the contents of an Arraylist

Posted 30 September 2013 - 09:27 PM

Ιn your Stuff class you have to override method toString() so it returns an informative representation of your object that is easy for a person to read.

public String toString() {
       return your data..;
}




Then in your for loop every object in your list should call that method.

This post has been edited by ChrisNt: 30 September 2013 - 09:33 PM

Was This Post Helpful? 1
  • +
  • -

#3 mushkyn   User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 42
  • Joined: 28-March 13

Re: Displaying the contents of an Arraylist

Posted 30 September 2013 - 10:35 PM

View PostChrisNt, on 30 September 2013 - 09:27 PM, said:

Ιn your Stuff class you have to override method toString() so it returns an informative representation of your object that is easy for a person to read.

public String toString() {
       return your data..;
}




Then in your for loop every object in your list should call that method.


Thanks for the response but I think I need a little more direction. I tried to create a toString method in the Stuff class but I don't think I did this right. But even so, I'm not sure how to call it from the loop (errors about static variables, tried changing static context to no avail.

Is it even necessary to use the toString method if I have setters and getters in the Stuff class?
public class Stuff
{ 
    private String A;
    private String B;
    private String C;    
    public Stuff()
    {
        A= ""; 
        B= "";
        C= ""; 
    }
    
    public Stuff (String a, String b, String c)
    {
        A= a;
        B= b; 
        C= c;
    } 
    
    public String toString(String a, String b, String c) {
        String answer= a+b+c;
        return answer;
    }

    public String getA(){return A;}
    public void setA(String newA){A=newA;} 
    public String getB(){return B;}
    public void setB(String newB){B=newB;}
    public String getC(){return C;}
    public void setC(String newC){C=newC;}   
} 



Was This Post Helpful? -1
  • +
  • -

#4 schutzzz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: Displaying the contents of an Arraylist

Posted 30 September 2013 - 11:20 PM

I see it's a duplicate thread so i'll just try and post in this one too. Your toString method shouldn't have any parameters. Also, don't forget to override it,

@Override
public String toString() {
    return str;
}



You don't necessarily have to say String answer = ... you can just use return ...

This post has been edited by schutzzz: 30 September 2013 - 11:20 PM

Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,856
  • Joined: 19-March 11

Re: Displaying the contents of an Arraylist

Posted 30 September 2013 - 11:35 PM

Deleted the dupe. mushkyn, please try to avoid duplicate threads in future. Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,856
  • Joined: 19-March 11

Re: Displaying the contents of an Arraylist

Posted 30 September 2013 - 11:52 PM

View Postmushkyn, on 01 October 2013 - 12:35 AM, said:

Thanks for the response but I think I need a little more direction. I tried to create a toString method in the Stuff class but I don't think I did this right. But even so, I'm not sure how to call it from the loop (errors about static variables, tried changing static context to no avail.



toString is inherited from Object, so all classes have it (they all inherit from Object). The main reason for this - probably the only reason for this - is that the println method will call an object's toString method if it is given an object as an argument. In a nutshell, this allows you to use any old object as the argument to toString, and it won't fail. (this is a bit of a simplification, roll with it and look it up later if you want)
I say it won't fail - that doesn't mean it'll work. If you haven't overridden the toString you inherited from Object, you get the Object version which, as you noticed, it transparently derived from the name of the class and its location on the heap. Since neither you nor anyone else can access that object on the heap using that information, it's only useful in that it's a guaranteed unique representation of the object.


As has already been pointed out, the correct signature for this method is public String toString() with no arguments. It's perfectly legitimate to have a method called toString that takes arguments. However, this will not be called when you try to print your argument, because it isn't overriding the inherited method. So that's why your attempt didn't work.


Quote

Is it even necessary to use the toString method if I have setters and getters in the Stuff class?


The println method doesn't know to use your getters. It couldn't possibly know, for example, which getters to use, if any, or how to really use them. All it knows is that if it gets an object, it'll try toString. And because all objects inherit from Object, that'll work, so it's happy.

As for getters and setters - why do you have them? What purpose are they serving?
In my experience, getters and setters are a code smell: they indicate the presence of one or more of a set of standard design flaws. Try to develop the habit of programming without them. It'll help your understanding of object-oriented programming.
Was This Post Helpful? 1
  • +
  • -

#7 mushkyn   User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 42
  • Joined: 28-March 13

Re: Displaying the contents of an Arraylist

Posted 01 October 2013 - 01:27 AM

View Postjon.kiparsky, on 30 September 2013 - 11:52 PM, said:

As for getters and setters - why do you have them? What purpose are they serving?
In my experience, getters and setters are a code smell: they indicate the presence of one or more of a set of standard design flaws. Try to develop the habit of programming without them. It'll help your understanding of object-oriented programming.


Thanks for this detailed response. The only reason I had getters and setters is because my college class encourages the use of them. I realize I was not using them at first, however with playing around, I came up with this display method that accomplishes what I was aiming for using the getters:

public static void displayStudents(ArrayList studentList)
    {
        for (int i=0; i<listOfStuff.size(); i++)
        {
            Object stuff= listOfStuff.get(i);
             
            System.out.println(((Stuff)stuff).getA());
            System.out.println(((Stuff)stuff).getB());
            System.out.println(((Stuff)stuff).getC());
            
            
            //System.out.print(toString()); is this close?
        }
    }




What do you guys think of this, any downsides to it? It seems to do exactly what I wanted though I'd still like to learn the other way suggested. I still don't know how to call the toString method in my main class though ("non-static method cannot be referenced...")
Was This Post Helpful? 0
  • +
  • -

#8 schutzzz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: Displaying the contents of an Arraylist

Posted 01 October 2013 - 01:43 AM

Nothing should be static but main. You can put everything in the constructor and create an instance of that in main. i.e.

public class Driver {

    Driver() {
        ...
        arrayList.add(stuffObj);
        System.out.println(arrayList.get(...));
    }

    public static void main(String[] arguments) {
        Driver driver = new Driver();
    }

}



public class Stuff {

    private int a;

    public Stuff(int a) {
        ...
    }

    @Override
    public String toString() {
        return ...
    }

}


This post has been edited by schutzzz: 01 October 2013 - 01:43 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1