• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Help with ArrayList?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i am trying to add these pieces of data into an ArrayList however i just want to show what i have done so far because i think i am going the right way about it but then i could be wrong :/

This is the question anyway i am answering:


 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

orry kaplan wrote:Hi, i am trying to add these pieces of data into an ArrayList however i just want to show what i have done so far because i think i am going the right way about it but then i could be wrong :/

This is the question anyway i am answering:


 
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to have a Data class that holds a name and an age. Might be better if this was renamed to something more meaningful, such as 'Person'.
On line 10 you have

This should probably be more like

because 'Object' doesn't give you robust type checking.
Then you'll notice that arr.add(21) etc. all break. That is because you'll need to be adding Persons.
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You seem to have a Data class that holds a name and an age. Might be better if this was renamed to something more meaningful, such as 'Person'.
On line 10 you have

This should probably be more like

because 'Object' doesn't give you robust type checking.
Then you'll notice that arr.add(21) etc. all break. That is because you'll need to be adding Persons.



Thanks for the help, but would you say what i have done so far for this question is correct?
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

orry kaplan wrote:Thanks for the help, but would you say what i have done so far for this question is correct?


It may compile, I can't tell if it would run because you provided incomplete code, but if I were your instructor I'd say no. If you've already gotten to the chapter on ArrayList then you should have already understood how to use constructors and do simplistic class definitions. You have a 'Data' class, where did it come from? What does the source code look like?
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 7 you use 'Data' correctly. You have to use the same approach with your 'arr.add(...)' calls.
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

orry kaplan wrote:Thanks for the help, but would you say what i have done so far for this question is correct?


It may compile, I can't tell if it would run because you provided incomplete code, but if I were your instructor I'd say no. If you've already gotten to the chapter on ArrayList then you should have already understood how to use constructors and do simplistic class definitions. You have a 'Data' class, where did it come from? What does the source code look like?



I have a sub class called Data that looks like so:


And my code i have so far but not sure if going the right way about it is like so:

>
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the compiler should complain with this
arr.add("Fred",21);
You are half way there. Make a new Data object with "Fred" and 21 and then 'add()' that new object.
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line will work but will will only print the hash of the object, not the object's data in a meaningful format.

You'll need to add

method to Data to return a meaningful String, something similar to your Print() method but returning a String instead of printing it out.
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Well, the compiler should complain with this
arr.add("Fred",21);
You are half way there. Make a new Data object with "Fred" and 21 and then 'add()' that new object.



>
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Excellent.



Thanks for the help. However when i print i get these results:

(Fred,21) (Fred,21) (Fred,21) Data@1540e19d
Data@677327b6
Data@14ae5a5
[Data@1540e19d, Data@677327b6, Data@14ae5a5]

i am sure these are the wrong results i should be looking for
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the hash I was mentioning. Not very useful is it?
Data@14ae5a5

You'll need a toString() method added to Data to provide meaningful formatting.
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:This is the hash I was mentioning. Not very useful is it?
Data@14ae5a5

You'll need a toString() method added to Data to provide meaningful formatting.



Would this also change the fact that (Fred, 21) has been duplicated 3 times?
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

orry kaplan wrote:Would this also change the fact that (Fred, 21) has been duplicated 3 times?


The duplication is because lines 11, 13, and 15 are all printing out 'x'.
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

orry kaplan wrote:Would this also change the fact that (Fred, 21) has been duplicated 3 times?


The duplication is because lines 11, 13, and 15 are all printing out 'x'.



Thanks, i didn't even notice haha. But that part where you said to print toString, i am still a little confused about how to do this?
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Google "java tostring tutorial" you'll find this
http://www.dreamincode.net/forums/topic/209515-basics-of-tostring/





i still dont know if im using the toString correctly but does this seem the correct way of using it. i read the page you sent and a few others btw
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove line 20. I'm not even sure what this line would do. Does it give a compile error?

Line 21 would work or you could keep your loop:
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A nit-pic:
An ArrayList is usually referred to as a 'list', not an array. Therefore your variable 'arr' would be clearer as 'list'.
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Line 21 would work or you could keep your loop:


You could also use the newer style of for() loops:
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:Line 21 would work or you could keep your loop:


You could also use the newer style of for() loops:



Could you please explain what actually goes into the toString? would it be toString(Data)    ?
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The tricky part here is that System.out.println() can only print Strings, so anything you put in the parenthesis has to be converted to a String if it isn't already. For objects it would implicitly call the object's toString() method. If you wanted to be verbose (i.e. non-implicit) you could have written this:

Directly calling toString() like this works but most people let the compiler insert it implicitly.

So, you have a reference named 'data' which refers to an object of type Data. This object contains 'name' and 'age'. When toString() is called it has access to the values stored in 'name' and 'age' for that particular object. It is toString()'s job to present those values in a meaningful way.

Does this help?
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:The tricky part here is that System.out.println() can only print Strings, so anything you put in the parenthesis has to be converted to a String if it isn't already. For objects it would implicitly call the object's toString() method. If you wanted to be verbose (i.e. non-implicit) you could have written this:

Directly calling toString() like this works but most people let the compiler insert it implicitly.

So, you have a reference named 'data' which refers to an object of type Data. This object contains 'name' and 'age'. When toString() is called it has access to the values stored in 'name' and 'age' for that particular object. It is toString()'s job to present those values in a meaningful way.

Does this help?



i understand what you are saying. you have cleared it up pretty well so thank you. but that code snip you put above, is that the solution to this? because even though i get that it cannot be printed unless it is toString, is there a way to print this list in a readable format without it as it seems a little confusing to use all the time.
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

orry kaplan wrote:That code snip you put above, is that the solution to this? because even though i get that it cannot be printed unless it is toString, is there a way to print this list in a readable format without it as it seems a little confusing to use all the time.


Using toString() is considered the proper way to do it. You could have been extremely verbose like this:

This is not the path usually chosen because of the risks of errors being introduced during maintenance and modification. Also, toString() is quite often used during the debugging stage of development.

Note that inserting parenthesis, commas, field names (e.g. "Name:") into the String are all up to the designer. Whatever you need for meaningful output.
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You could have been extremely verbose like this:


In your case, because you have a print() method, you could have done this:

However, you COULD NOT have done this without a toString():
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

orry kaplan wrote:That code snip you put above, is that the solution to this? because even though i get that it cannot be printed unless it is toString, is there a way to print this list in a readable format without it as it seems a little confusing to use all the time.


Using toString() is considered the proper way to do it. You could have been extremely verbose like this:

This is not the path usually chosen because of the risks of errors being introduced during maintenance and modification. Also, toString() is quite often used during the debugging stage of development.

Note that inserting parenthesis, commas, field names (e.g. "Name:") into the String are all up to the designer. Whatever you need for meaningful output.


/
So can the toString be used on this program effectively
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:You could have been extremely verbose like this:


In your case, because you have a print() method, you could have done this:

However, you COULD NOT have done this without a toString():





i have finalised this thinking i have finished the problem. do you think this seems correct?
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep. That should work.

Do you need to print age as well?
 
orry kaplan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Yep. That should work.

Do you need to print age as well?



Yeah I do but I wasn't sure how to add it in too, unless I duplicate the print line with GetName and change the second one to GetAge?
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the String concatenation operator (+)
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic