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

    Default Add lines of a matrix in arrayList

    Hello guys. I ve stuck to the following issue. Actually I m having a 2D array of integers 1850x3 and I want to create an new array using only lines which include a specific integer. I m trying to parse these lines into an arrayList but I ve got a few troubles. Any tips to solve out this problem??


    Java Code:
    List<Integer> x =  new ArrayList<Integer>();
    
                          for (int i = 0; i < Faces.length; i++) {
    
                                x.add(Faces[i][0]);
                                x.add(Faces[i][1]);
                                x.add(Faces[i][2]);
    
                       
                    }
    Last edited by zenitis; 05-04-2011 at 02:07 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What's the issue? This look like it should work. Are you getting errors? Incorrect output?

  3. #3
    Inferno719 is offline Member
    Join Date
    May 2011
    Posts
    14
    Rep Power
    0

    Default

    Newbie attempting to answer the question. Not sure I understand, but wouldn't it work as:

    Java Code:
    List<Integer> x =  new ArrayList<Integer>();
    
    for (int i = 0; i < Faces.length; i++) 
    {
        j = i - 1;
        x.add(Faces[i][j]);
    }
    I meant int j = i - 1 =P
    Last edited by Inferno719; 05-04-2011 at 02:25 AM.

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

    Default

    :cool: sorry my mistake the problem is already solved. Now I want to calculate the number of unique integer elements of the list. Any suggestions?

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    it may be helpful to use a map. A set will also allow you to have a unique list of all the elements. But no count. You can try doing something like
    Java Code:
    Map<Integer, Integer>
    where the key is the integer found, and the value is the count of that integer.


    Your attempt inferno doesn't declare j, it just refers to it. It wouldn't help. The op however; could have done something like
    Java Code:
    x.add(Faces[i][j]);
    I believe the original post was a little edited though. Do your best yo help out around her inferno. If you are wrong, I or someone else will correct you. If the correction seems harsh don't take offense, just use it as a learning experience.

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

    Default

    Easy guys for the sake of java. What i finally try to do it is something like that

    Java Code:
     List<Integer> x =  new ArrayList<Integer>();
                List<Integer> valenceList =  new ArrayList<Integer>();
                List<List<Integer>> conVert = new ArrayList<List<Integer>>();
    
    
               for (int q = 1; q <= 2; q++) {
                
    
                    for (int i = 0; i < Faces.length; i++) {
    
                        for (int j = 0; j < 2; j++) {
    
                            if (Faces[i][j] == q) {
    
                                x.add(Faces[i][0]);
                                x.add(Faces[i][1]);
                                x.add(Faces[i][2]);
    
                           }
                        }
                    }
                     removeDuplicateWithOrder((ArrayList) x);
                     valenceList.add(x.size() -1);
                     conVert.add(x);
                     System.out.println(conVert);
                     x.clear();
                      //System.out.println(conVert);
                }
    Ignore the most code my finally I have a different list x every iteration and I want to parse it in the 2DarrayList conVert so I use the conVert.add(x); but my problem is on x.clear();. When I print conVert before clear conVert is ok but when i print it after clear besides x also and conVert is cleared. What can I do for this?

Similar Threads

  1. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  2. Multiple lines in arrayList
    By Lund01 in forum Advanced Java
    Replies: 6
    Last Post: 10-15-2010, 01:34 PM
  3. Add Matices in ArrayList <Matrix>?
    By spatel14 in forum New To Java
    Replies: 19
    Last Post: 04-07-2010, 11:49 AM
  4. [SOLVED] Writing ArrayList to Text File on seperate Lines
    By shinjitsunohana in forum New To Java
    Replies: 9
    Last Post: 08-27-2008, 06:53 PM
  5. Help with matrix
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:37 AM

Posting Permissions

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