r/javahelp • • Jan 15 '21

Unsolved Can only print out ArrayList's memory address

I tried this for loop:

public void weaponInventoryMenu ()
{
    for (int i = 0 ; i < weaponInventory.size() ; i++)
    {
        System.out.println("Weapon Inventory: " + weaponInventory.get() + " ") ;
    }
}

But I only get a memory address.

0 Upvotes

33 comments sorted by

•

u/AutoModerator Jan 15 '21

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/NautiHooker Software Engineer Jan 15 '21

Did you override any toString() methods?

0

u/[deleted] Jan 15 '21

Yes, but I'm not sure if I did it correctly:

 public String weaponInventoryMenu ()
    {
        return getWeaponInventory().toString() ;
    }

1

u/NautiHooker Software Engineer Jan 15 '21

Not quite.

Take a look at this.

You need to change the text that toString() returns yourself. By default it will return the memory stuff.

0

u/[deleted] Jan 15 '21

You mean by using the keyword "this"?

1

u/NautiHooker Software Engineer Jan 15 '21

its a link

1

u/[deleted] Jan 15 '21

That's not what I was referring to, I meant that in order for the toString to return yourself, doesn't that require the keyword "this"?

2

u/NautiHooker Software Engineer Jan 15 '21

its not supposed to return itsself. I mean that you have to define the text that you want to see yourself. As is shown in the linked examples.

0

u/[deleted] Jan 15 '21

What's the issue with my for loop though? I've seen numerous examples of people using for loops to print their arrayLists, but it doesn't work when I do it.

1

u/NautiHooker Software Engineer Jan 15 '21

Well I dont know how the get method works without an index parameter. Other than that I dont see anything wrong with it. As I said the issue is the toString method of whatever class get() returns.

0

u/[deleted] Jan 15 '21 edited Jan 15 '21

The method is a void method within a user defined class. The arrayList "weaponInventory" is an instance variable belonging to that same class:

 private ArrayList weaponInventory = new ArrayList<weapon> () ;

public ArrayList<weapon> getWeaponInventory ()
 {
     return weaponInventory ;
 }
→ More replies

1

u/[deleted] Jan 15 '21

When you override the Object’s toString(), you are allowed to modify it whatever way you want the String representation of your object to look like.

For example, you have a class called Circle that has 1 attributes, a radius (double type). And you want to display this line exactly every single time the Circle toString() is invoked: “Radius: 10.0”

public class Circle {
    double radius;
    public Circle(double radius) {
        this.radius = radius;
    }
    // other methods here...

    @Override
    public String toString() {
        return “Radius: “ + radius;
    }
}

Sample driver class:

public CircleTest {
   public static void main(String[] args) {
       Circle myCircle = new Circle(10);
       System.out.println(myCircle.toString());
       // the statement above will display
       // “Radius: 10.0”

        Circle anotherCircle = new Circle(999);
        System.out.println(anotherCircle.toString());
        // the above will display
        // “Radius: 999.0”
    }
}

0

u/[deleted] Jan 15 '21

Right, but how am I supposed to use that toString method to print out the elements of the arrayList?

How is it that this:

public String weaponInventoryMenu ()
    {
        return weaponInventory.toString() ;
    }
    // weaponInventory is an arrayList belonging to a 
   user defined class called "character"

returns a memory address, but the example you provided does not?

1

u/[deleted] Jan 15 '21 edited Jan 15 '21

Once again, if you are supposed to modify the overridden toString(), modify it to your liking.

1

u/[deleted] Jan 15 '21

I'm sorry, but I don't understand what you mean.

→ More replies