Results 1 to 8 of 8
  1. #1
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default 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...

    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;
        }
    
       
        
       
    
    }
    Tips or suggestions are allways welcome :)

    Thanks in advance,

    Dieter
    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...

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    What's the problem? You didn't really specify a question.
    What is code doing vs what it should be doing?

  3. #3
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    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...

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    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:

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    ...

    To retreive the DuoArrayList related values,do like this below.


    DuoArraylist obj = (DuoArraylist)ex.get(1);

    ..
    Don't do any of that. Otherwise there is no point in using the generics in the first place.

    Rather do
    Java Code:
    DuoArraylist<Integer>  duo= ex.get(1);

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    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
    Ramya
    Ramya:cool:

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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

  8. #8
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    the null value is someting i forgot to get out when i was just writing an arraylist with different values in the previous exe.

    Java Code:
    duo= ex.get(1);
    where should i put that in code? in or out the list?

    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,

    Dieter
    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...

Similar Threads

  1. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 07:58 PM
  2. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:08 AM
  3. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-10-2008, 11:08 PM
  4. Generics
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 09:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •