ArrayList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shana07
    Contributor
    • Jan 2007
    • 280

    ArrayList

    Hi again, allow me to ask List question by giving one example and please comment:

    1. I run a program with first input a = 1; and its result is stored in a list 'ifs'
    which giving ifs.size() = 4

    2. Then it repeats (loop) with second input a = 6; and wanted to store its result in same variable 'ifs'. their size is same. is it possible to do so?

    Currently, by running second input it's giving me result ifs.size()=8
    (total size for both inputs).

    My aim is not to increase the 'ifs' size...it's like to create them in sublist such this:
    ifs[i].size = 4
    ifs[ii].size = 4
    ifs[iii].size = 4
    ...
    Code:
    java.util.List ifs = new java.util.ArrayList();
    Please advise
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Not sure what you are trying to do - do you want an array of ArrayLists?
    Code:
        ArrayList[] list=new ArrayList[10];
        for(int i=0; i<list.length;i++)
          list[i]=new ArrayList();
        list[0].add(4);
        list[1].add(4);
        System.out.printf("size " + list[0].size());

    Comment

    • hirak1984
      Contributor
      • Jan 2007
      • 316

      #3
      but where is the problem?

      did you get any errors?

      Originally posted by shana07
      Hi again, allow me to ask List question by giving one example and please comment:

      1. I run a program with first input a = 1; and its result is stored in a list 'ifs'
      which giving ifs.size() = 4

      2. Then it repeats (loop) with second input a = 6; and wanted to store its result in same variable 'ifs'. their size is same. is it possible to do so?

      Currently, by running second input it's giving me result ifs.size()=8
      (total size for both inputs).

      My aim is not to increase the 'ifs' size...it's like to create them in sublist such this:
      ifs[i].size = 4
      ifs[ii].size = 4
      ifs[iii].size = 4
      ...
      Code:
      java.util.List ifs = new java.util.ArrayList();
      Please advise

      Comment

      • shana07
        Contributor
        • Jan 2007
        • 280

        #4
        Originally posted by horace1
        Not sure what you are trying to do - do you want an array of ArrayLists?
        Code:
            ArrayList[] list=new ArrayList[10];
           [B] for(int i=0; i<list.length;i++)[/B]     
          list[i]=new ArrayList();
            list[0].add(4);
            list[1].add(4);
            System.out.printf("size " + list[0].size());
        Maybe yes, but I'm still not sure...I applied that into my code (looping):
        Code:
        ifs1[ch]=new ArrayList();                    
        ifs1[ch].add(new Entry(position, value));                    
        System.out.println("SIZE: " +ifs1[ch].size());  
        System.out.println("ENTRIES: " +ifs1[ch]);
        it's giving result size = 1 & entry is also one.
        Is it possible to total up all the entries?

        As per bold in your code above, the length given is 10?

        Comment

        • shana07
          Contributor
          • Jan 2007
          • 280

          #5
          Originally posted by hirak1984
          but where is the problem?

          did you get any errors?
          There's no problem yet friend. Just finding info either possible for me to get many ArrayLists.Beca use by having looping, this code:
          Code:
          ifs1.add(new Entry(position, value));
          will keep increasing the Entry size.
          urgh, sorry for confusing you guys..gimme a break ;)

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by shana07
            Maybe yes, but I'm still not sure...I applied that into my code (looping):

            As per bold in your code above, the length given is 10?
            10 is the length of the array, i.e.
            Code:
                for(int i=0; i<list.length;i++)     
                  list[i]=new ArrayList();
            I only add one element to each list so size() would be 1.
            I think you need to specify exactly what you want to do then perhaps we can advise.

            Comment

            Working...