• Post Reply Bookmark Topic Watch Topic
  • New Topic

Purpose of composition  RSS feed

 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to find out, why it is used, I dont see it makes any sence. It even fails to transfer primityves ( int, double, byte ).

I have made two ArrayLists. I dont understand why console is empty. Console should show "10 5 4" . But it is empty. ( I runned without 30th line . )
Also why 30th line drops mistake?



 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some.
0444444.png
[Thumbnail for 0444444.png]
 
Erik Kan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bob!

First thing: You shouldn't try to define your own class for Byte! Java already has a class named Byte in its java.lang package, and it looks like it does exactly what you need it to do.

Secondly: Let's look at what exactly your code does for now. First, you declare an ArrayList of Medis, but don't add any objects to it. You then print the empty list.

Then, you make a list of your defined class Byte. You try to add an int to it, but since the primitive int is not a Byte, you can't add it to the list.

You try to print two lists, but since both are empty at that point, nothing shows up.

I hope this helps!
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erik Kan wrote:. . . Then, you make a list of your defined class Byte. You try to add an int to it, but since the primitive int is not a Byte, you can't add it to the list.
No, you can't You might be able to box an int to java.lang.Integer, but you cannot convert it to an instance of your own class. So the generics will not allow the code to compile
You try to print two lists, but since both are empty at that point, nothing shows up. . . .
The reason nothing shows up is that the code won't compile.

You can only say myList.add(4) if the List will accept Integers (or their supertypes). So this will work because the 4 is boxed to an Integer:-but any attempt to add 4 to a List<Byte> will fail to compile. You cannot combine other sorts of automatic conversion with boxing.
As you have already been told, don't create classes with the same names as classes in the java.lang package.

And welcome to the Ranch
 
Erik Kan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And welcome to the Ranch


Thanks!

The reason I didn't mention the compiler error was because Bob said he ran his code sans line 30.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you declare an ArrayList of Medis, but don't add any objects to it. You then print the empty list.


Is it possible to fill ArrayList in diffrend ways ? ( I know this way:      ing.add ("nulbl"); )

Would be interesting to see ArrayList filled with object.
 
Erik Kan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method of adding is correct. However, you need to make sure that you are adding the correct type of item to the list. Since your ArrayList "me" is a list of Medis, you could try

<code>
me.add(new Medis());
</code>
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Erik Kan:

You almost got the code tags correct.  All you need is to use square brackets ([]) instead of angle brackets (<>).  Here is a link on how to UseCodeTags.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayList medis print nonsence to console.
Still having problem with second ArrayList. ( to print int value ).



088.png
[Thumbnail for 088.png]
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:ArrayList medis print nonsence to console. . . .
No it doesn't That isn't nonsense at all; it is simply something unfamiliar to you. Read what it says about the toString() method in that link. You must override toString if you want something different.
Please don't add screenshots because they can be difficult to read.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:. . . . Still having problem with second ArrayList. ( to print int value ). . . .
You told it to add null, so it added null. If you want to add something different, and it.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!