Results 1 to 6 of 6

Thread: Initialize an ArrayList

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Initialize an ArrayList

    Hello,
    I must make additions to an ArrayList. However, these additions are not in chronological order, I mean I have to say first of index 5, then 2, then 1 (the random order of addition is not known in advance). Here is my code
    Code:
    List lp = new ArrayList(10);
            lp.add(5, 2);
    and I have an overflow exception index
    Code:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5 Size: 0
    I want to know how to initialize a ArrayList in a program so that it can work properly.

  2. #2
    Join Date
    Nov 2009
    Posts
    330

    Re: Initialize an ArrayList

    Hello,
    What exactly is the purpose of the maneuver? If the list of predefined size, why not directly use an array? I think here is the solution for you.
    Code:
    java.util.List t = new java.util.ArrayList(java.util.Collections.nCopies(10, null));
    t.set(5,2); / / position the element of index 5
    System.out.System.out.println(t);
    Just try this, if it works well and good it not then you can post your query that you get.

  3. #3
    Join Date
    Dec 2009
    Posts
    263

    Re: Initialize an ArrayList

    Hello,
    I use no table because later I'll make a lot of operation. In summary I must travel a bit and insert hashmap index columns of the hashmap which are equal to one and not zero in my sleep as ArrayList of ArrayList. Here is my modified code.
    Code:
    java.util.List lp = new java.util.ArrayList(java.util.Collections.nCopies(nbActivite, null));
     
            for (int i =0; I <10; I + +) {   / / Traverse the lines da hash MA containing vectors
                List vectmp = (Vector) MA.get(i); 
                for (int j =0J <nbActivite j + +){   / / Traverse the vector of each line i
                    int e = ((Integer)(vectmp.get(j))).intValue();  / / E = element of M.A. (i, j)
     
                    if (e ==1){
    
                           List listTemp = new ArrayList();
    
                           lpTemp = (ArrayList) lp.get(j); / / I'll get the list in my lp to add the new item to add
    
                           listTemp.add(p);
    
                           lp.add(j lpTemp);
                   }
                }
            }
    Help me with this.

  4. #4
    Join Date
    Nov 2009
    Posts
    335

    Re: Initialize an ArrayList

    Hello,
    Everything is in initializing the List if it is a list of ArrayList. Look at the following lines in the code.
    Code:
    java.util.List lp = new 
    java.util.ArrayList(java.util.Collections.nCopies(nbActivite, new )
    You have to include this in your program, just modify your code by including this and then run the program and see if it works.

  5. #5
    Join Date
    Dec 2009
    Posts
    263

    Re: Initialize an ArrayList

    Hello,
    yes, i think you are correct with this, but it remains a problem. I feel that when I made
    Code:
    lstmp = (ArrayList)listP.get(j);
    lstmp.add(p);
    listP.add(j lstmp);
    The lstmp, it add me in all my index lp not only to location j. I think here is the error in my code, though I am not sure about it. If you can understand this then please help me with this.

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Initialize an ArrayList

    Hello,
    Actually, it creates a list containing n times the same ArrayList. So starting afresh solution 'java.util.List = new java.util.ArrayList lp (java.util.Collections.nCopies (nbActivite, null)); "
    I think you may need to change the program a little.
    Code:
    List lstmp =  (ArrayList) listP.get(j); / / I'll get the list in my listP to add the new item to add
    if(lstmp == null) { 
        / / no list, it initializes
        lstmp = new ArrayList();
        / / Assign the new list listP
        listP.set(j listPTemp);
    }
    
    lstmp.add(p);
    / / listP.add (j lstmp) No need to add because it is already

Similar Threads

  1. Using an ArrayList
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 12:56 PM
  2. Finding items with ArrayList
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 02:03 AM
  3. ArrayList - Sort by Index
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 05:22 AM
  4. Multithreading and ArrayList
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 05:16 AM
  5. ArrayList not working
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 04:18 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,710,835,259.17995 seconds with 16 queries