Results 1 to 8 of 8
Thread: generics inside collection
- 09-23-2009, 02:27 PM #1
generics inside collection
Heyaa,
Today i saw generics and collections at school.
But i'm trying to combine those tgetter.
but i don't know where or how to give values to it in thi situation.
got this code so far..
Java Code:import java.util.*; /** * * @author Dieter */ public static void main(String[] args) { DuoArraylist<Integer> n1 = new DuoArraylist<Integer>(3,5); List <DuoArraylist<Integer>> ex = new ArrayList<DuoArraylist<Integer>>(); ex.add(null); ex.add(n1); System.out.println(ex); }
Now for the DuoArraylist class...
Tips or suggestions are allways welcome :)Java Code:public class DuoArraylist <E extends Number> { private E arrayItem1; private E arrayItem2; public DuoArraylist(E arrayItem1, E arrayItem2) { this.arrayItem1 = arrayItem1; this.arrayItem2 = arrayItem2; } public E getArrayItem1() { return arrayItem1; } public void setArrayItem1(E arrayItem1) { this.arrayItem1 = arrayItem1; } public E getArrayItem2() { return arrayItem2; } public void setArrayItem2(E arrayItem2) { this.arrayItem2 = arrayItem2; } }
Thanks in advance,
DieterProgramming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...
- 09-23-2009, 02:48 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What's the problem? You didn't really specify a question.
What is code doing vs what it should be doing?
- 09-23-2009, 03:24 PM #3
i want to display the values i give to DuoArraylist in the Arraylist
but it displays
Java Code:run: [null, arraylistapp.DuoArraylist@19821f] BUILD SUCCESSFUL (total time: 0 seconds)
Last edited by Dieter; 09-23-2009 at 03:27 PM.
Programming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...
- 09-23-2009, 03:47 PM #4
Hi,
As per the code u have written,It has printed perfectly.
You have added null and DuoArrayList object.
My question is why u added null at first index?
To retreive the DuoArrayList related values,do like this below.
DuoArraylist obj = (DuoArraylist)ex.get(1);
After getting the object try to call the getter method to print ur inputted values.Ramya:cool:
- 09-23-2009, 03:56 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-23-2009, 03:59 PM #6
Hi ro35198x,
U are right.Great catch!!!. forgotten when I type....
Hi Dieter,
without typecasting try to retreive DuoArraylist<Integer> duo= ex.get(1); like r0... told.
-Regards
RamyaRamya:cool:
- 09-23-2009, 08:01 PM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
isn't "array list" a bit of a misnomer, considering you don't implement anything with an array?
anyways, your problem is that you're printing out a list containing lists (DuoArraylist to be exact). to print the items in the DuoArraylists, you have to do that separately.
you're just mixing up your lists... draw it out and it'll make sense
- 09-23-2009, 10:53 PM #8
the null value is someting i forgot to get out when i was just writing an arraylist with different values in the previous exe.
where should i put that in code? in or out the list?Java Code:duo= ex.get(1);
ah well i will try it tmorrow if i got the time.
Thanks alot to you all for making it more clear to me now :)
All the best,
DieterProgramming today is a race between software engineers striving to build bigger and better idiot proof programs,and the Universe trying to produce bigger and better idiots...
Similar Threads
-
generics
By tascoa in forum Forum LobbyReplies: 2Last Post: 10-09-2008, 07:58 PM -
Help w/ generics
By Hollywood in forum New To JavaReplies: 2Last Post: 02-16-2008, 03:08 AM -
Generics
By sireesha in forum New To JavaReplies: 2Last Post: 01-10-2008, 11:08 PM -
Generics
By eva in forum New To JavaReplies: 2Last Post: 01-04-2008, 09:10 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks