Results 1 to 4 of 4
  1. #1
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default Cast an 2d array to ArrayList

    I ve got a 2d array and I want to cast it in an 2d arraylist. I ve create a function that cast an array to arraylist. My problem arises, when I tried to parse the whole 2d matrix to the arraylist. I use the following code:

    Java Code:
      double sums[][] = computeSums(lab, projections);
            ArrayList<ArrayList<Double>> lists = new ArrayList<ArrayList<Double>>();
            ArrayList<Double> nu = new ArrayList<Double>();
            System.out.println(sums[0].length);
            for (int i = 0; i < sums.length; i++) {
                
                ArrayList<Double> tt = toList(sums[i], nu);
                lists.add(tt);
     
            }
    The problem is that only the first matrix sums[0] is copied to the 2d arraylist sums.length times. How is is possible to store all the different sums matrices to the arraylist??

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Cast an 2d array to ArrayList

    Maybe you can provide the example data such as what is in the sums[][] so we can imagine the loop. Or maybe posting a complete code will be better.

  3. #3
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default Re: Cast an 2d array to ArrayList

    Basically, I ve found my bug. I ve to add to 2d arraylist just a clone of the list nu and after that it is possible to use the command nu.clear(); and clear the temporal list.
    Java Code:
            
            double sums[][] = computeSums(lab, projections);
            ArrayList<ArrayList<Double>> lists = new ArrayList<ArrayList<Double>>();
            ArrayList<Double> nu = new ArrayList<Double>();
            System.out.println(sums[0].length);
         
            int i=0;
            do{       
                toList(sums[i], nu);
                lists.add((ArrayList<Double>) nu.clone());
                nu.clear();
            i++;    
           
            }while(i<sums.length);

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Cast an 2d array to ArrayList

    Another possibility. Here is your code.

    Java Code:
    double sums[][] = computeSums(lab, projections);
          ArrayList<ArrayList<Double>> lists = new ArrayList<ArrayList<Double>>();
          ArrayList<Double> nu = new ArrayList<Double>();
          System.out.println(sums[0].length);
          for (int i = 0; i < sums.length; i++) {
               
              ArrayList<Double> tt = toList(sums[i], nu);
              lists.add(tt);
    }

    If you are using the same reference, nu in which to store the array then you will continue to store only that reference. So you need to move the nu list creation, inside of your for loop. In fact, you don't even need to return the reference. Just keep adding nu. The other option would be to allocate the list in the toList method and then return that.

    Regards,
    Jim
    Last edited by jim829; 01-23-2014 at 04:36 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. HashSet cannot be cast to java.util.ArrayList
    By vector_ever in forum New To Java
    Replies: 8
    Last Post: 08-07-2013, 07:30 PM
  2. Replies: 1
    Last Post: 06-19-2012, 03:55 AM
  3. Convert Vector to Generic Array Cast Exception
    By officialhopsof in forum New To Java
    Replies: 3
    Last Post: 01-04-2011, 04:49 AM
  4. 2D array of ArrayList (technically a 3D array)
    By Lil_Aziz1 in forum New To Java
    Replies: 3
    Last Post: 05-15-2010, 09:11 PM
  5. Array to ArrayList
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 01:46 AM

Tags for this Thread

Posting Permissions

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