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:
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.
orry kaplan wrote:Thanks for the help, but would you say what i have done so far for this question is correct?
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?
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 wrote:Excellent.
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.
orry kaplan wrote:Would this also change the fact that (Fred, 21) has been duplicated 3 times?
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'.
Carey Brown wrote:Google "java tostring tutorial" you'll find this
http://www.dreamincode.net/forums/topic/209515-basics-of-tostring/
Carey Brown wrote:Line 21 would work or you could keep your loop:
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:
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?
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.
Carey Brown wrote:You could have been extremely verbose like this:
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.
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():
Carey Brown wrote:Yep. That should work.
Do you need to print age as well?
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
|