Results 1 to 8 of 8
  1. #1
    whimsical is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Trouble with 2d ArrayList

    Hello all,

    First off, I'd like to say that this post does involve a homework assignment. I'm to write a class that (in the words of my professor):

    Write a class called ListMatrix that implements the BooleanMatrix interface. This implementation uses Java's built-in ArrayList class. Think of each matrix row as being an object of type ArrayList<Boolean>. Then the overall matrix is an ArrayList of these objects. In other words, the matrix is an object of type ArrayList<ArrayList<Boolean>>.

    Simple enough, right? Not for this dunce! Pretty much immediately after construction, when I call my get(int row, int col) method, I receive the following IndexOutOfBoundsException:

    Java Code:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    	at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    	at java.util.ArrayList.get(ArrayList.java:382)
    	at hw12.ListMatrix.get(ListMatrix.java:119)
    	at hw12.HW12.main(HW12.java:43)
    Java Result: 1
    I suspect I've done something incorrectly in my constructor, although after combing through lots of online examples, i just can't see what it is. I know that the error message is saying that I'm trying to get an index that isn't there (or at least I think that's what it's saying....), but when testing, i gave the parameters(3,3). then in the constructor i looped through and thought I set all nine to false. Sigh...anyway, here is the code I have so far:

    Java Code:
    public class ListMatrix implements BooleanMatrix {
        protected ArrayList<ArrayList<Boolean>> lm = new ArrayList<ArrayList<Boolean>>();
        
        protected int numRows;
        protected int numCols;
        protected boolean value;
        
        public ListMatrix(){
            
            this.numCols = 1;
            this.numRows = 1;
            this.value = false;
            for(int i = 0; i < 1; i++){
                lm.add(new ArrayList<Boolean>());
            }
            for(int i = 0; i < lm.size(); i++){
                for(int j = 0; j < lm.get(i).size(); j++){
                    lm.get(i).set(j, false);
                }
            }
            
        }
        
        public ListMatrix(int rows, int cols){
            
            this.numCols = cols;
            this.numRows = rows;
            this.value = false;
            
            for(int i = 0; i < rows; i++){
                lm.add(new ArrayList<Boolean>());
            }
            for(int i = 0; i < lm.size(); i++){
                for(int j = 0; j < lm.get(i).size(); j++){
                    lm.get(i).set(j, false);
                }
            }
        }
    
        
    
        @Override
        public int getNumberRows() {
            return this.numRows;
        }
    
        @Override
        public int getNumberCols() {
            return this.numCols;
            
        }
    
        @Override
        public void set(int row, int col) throws IndexOutOfBoundsException {
            ArrayList a = lm.get(row);
            a.set(col, true);
        }
    
        @Override
        public void clear(int row, int col) throws IndexOutOfBoundsException {
            lm.get(row).set(col, false);
        }
    
        @Override
        public void set(int row, int col, boolean value) throws IndexOutOfBoundsException {
            lm.get(row).set(col, value);
        }
    
        @Override
        public void toggle(int row, int col) throws IndexOutOfBoundsException {
            if(lm.get(row).get(col) == true){
                lm.get(row).set(col, false);
            }
            else if(lm.get(row).get(col) == false){
                lm.get(row).set(col, true);
            }
        }
    
        @Override
        public void setAll() {
            for(int i = 0; i < lm.size(); i++){
                for(int j = 0; j < lm.get(i).size(); j++){
                    lm.get(i).set(j, true);
                }
            }
            
        }
    
        @Override
        public void clearAll() {
            for(int i = 0; i < lm.size(); i++)
                for(int j = 0; j < lm.get(i).size(); j++){
                    lm.get(i).set(j, false);
                }
        }
    
        @Override
        public void setAll(boolean value) {
            for(int i = 0; i < lm.size(); i++){
                for(int j = 0; j < lm.get(i).size(); j++){
                    lm.get(i).set(j, value);
                }
            }
        }
    
        @Override
        public boolean get(int row, int col) throws IndexOutOfBoundsException {
            return lm.get(row).get(col);
        }
    
        @Override
        public int[][] getTruePositions() {
            int[][] truePos = new int[getNumberTrueValues()][2];
            for(int i = 0; i < lm.size(); i++){
                for(int j = 0; j < lm.get(i).size(); j++){
                    if(lm.get(i).get(j) == true){
                        truePos[i][0] = i;
                        truePos[i][1] = j;
                    }
                }
            }
            return truePos;
        }
    
        @Override
        public int[][] getFalsePositions() {
            int[][] falsePos = new int[getNumberFalseValues()][2];
            for(int i = 0; i < lm.size(); i++){
                for(int j = 0; j < lm.get(i).size(); j++){
                    if(lm.get(i).get(j) == false){
                        falsePos[i][0] = i;
                        falsePos[i][1] = j;
                    }
                }
            }
            
            return falsePos;
        }
    
        @Override
        public int[][] getPositions(boolean value) {
            int[][] posArray = null;
            if(value == true){
                posArray = new int[getNumberTrueValues()][2];
            }
            else if(value == false){
                posArray = new int[getNumberFalseValues()][2];
            }
            for(int i = 0; i < lm.size(); i++){
                for(int j = 0; j < lm.get(i).size(); j++){
                    if(lm.get(i).get(j) == value){
                        posArray[i][0] = i;
                        posArray[i][1] = j;
                    }
                }
            }
            return posArray;
        }
    
        @Override
        public int getNumberTrueValues() {
            int trueCount = 0;
            for(int i = 0; i < lm.size(); i++){
                for(int j = 0; j < lm.get(i).size(); j++){
                    if(lm.get(i).get(j) == true){
                        trueCount++;
                    }
                }
            }
            return trueCount;
        }
    
        @Override
        public int getNumberFalseValues() {
            int falseCount = 0;
            for(int i = 0; i < lm.size(); i++){
                for(int j = 0; j < lm.get(i).size(); j++){
                    if(lm.get(i).get(j) == false){
                        falseCount++;
                    }
                }
            }
            return falseCount;
        }
    
        @Override
        public int getNumberValues(boolean value) {
            int valueCount = 0;
            for(int i = 0; i < lm.size(); i++){
                for(int j = 0; j < lm.get(i).size(); j++){
                    if(lm.get(i).get(j) == value){
                        valueCount++;
                    }
                }
            }
            return valueCount;
        }
        
        @Override
        public String toString(){
            String s = " ";
            for(int row = 0; row < lm.size(); row++){
                for(int col = 0; col < lm.get(row).size(); col++){
                    s = s + lm.get(row).get(col) + " ,";
                }
            }
            return s;
        }
        
    }
    I'm sorry this post is so long. I wanted to give as much information as I could. If anyone could nudge me in the right direction I would be oh so grateful-I feel lost entirely.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Trouble with 2d ArrayList

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at hw12.ListMatrix.get(ListMatrix.java:119)
    At line 119 you trying to get an element from an empty arraylist (Size: 0)
    The code should test the length/size of the arraylist before trying to get elements from it.

    Add a println to show the values of i and j to see what the the code is doing.
    Also print out the size of the arraylist being accessed there.
    Last edited by Norm; 04-23-2012 at 02:45 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    whimsical is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Trouble with 2d ArrayList

    Thanks for your response. I know this will sound like a stupid question, but I'm not very good at this yet. Should I code the length/size testing in the constructor itself?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Trouble with 2d ArrayList

    First, did you print out the values of i and j and the sizes of the arraylists that were being used so you understand the data you are working with? When you see that it can help you see where to control the values of the indexes used to access the arraylists.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    whimsical is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Trouble with 2d ArrayList

    Thank you! Somehow, I missed the second part of your post, about inserting the println to help visualization. So I did that, and it did indeed make it easier to see what I need to do, because when I inserted the print statements like so:

    Java Code:
    for(int i = 0; i < rows; i++){ 
                lm.add(new ArrayList<Boolean>());
                System.out.println(i);
                System.out.println(lm.get(i).size());
            }
            for(int i = 0; i < lm.size(); i++){
                //System.out.println(i);
                for(int j = 0; j < lm.get(i).size(); j++){
                   // System.out.println(j);
                    lm.get(i).set(j, false);
                    
                }
            }
    it showed me that there is no size for the next for loop to go off of-all three of the elements have size 0! So I need to change the second part of the second for loop, where i < lm.size().

    I really like how you put your advice-it helped a lot! Thanks!! Stay tuned, I'm sure I'm not finished firing off strange questions for the evening...

  6. #6
    whimsical is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Trouble with 2d ArrayList

    I have a huge happy grin right now!!

    This is what I did(commented out now for testing)-maybe it will help some other noob down the road.

    Java Code:
     for(int i = 0; i < rows; i++){ 
                lm.add(new ArrayList<Boolean>());
                //System.out.println(i);
                //System.out.println("This is the size of lm: " + lm.size());
            }
            for(int i = 0; i < lm.size(); i++){
                //System.out.println(i);
                //System.out.println("this is # " + i + "time thru the inner loop.");
                for(int j = 0; j < lm.size(); j++){
                   // System.out.println(j);
                    lm.get(i).add(false);
                   // System.out.println("False has been added to lm" + i + "index " + j);
                    
                }
            }
    which made it easier to see that it wasn't the lm.size() part, but the lm.get(i).set(j, false) that was causing the problem. I guess you can't set what isn't there...

    Testing the methods now...Excitement! Thanks again!

  7. #7
    whimsical is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Trouble with 2d ArrayList

    Well, the happy grin held out until testing of the getPositions(value) method. That's ok. It'll get there. I did the same commenting trick-oh so awesome- and this is what I've done with the method. For simplicity's sake, I left the values of the 3 x 3 matrix at the default values of false:

    Java Code:
    @Override
        public int[][] getPositions(boolean value) {
            int[][] posArray = null;
            if(value == true){
                posArray = new int[getNumberTrueValues()][2];
            }
            else if(value == false){
                posArray = new int[getNumberFalseValues()][2];
            }
            for(int i = 0; i < lm.size(); i++){
                System.out.println("this is time # " + i + " thru the inner loop.");
                System.out.println();
                for(int j = 0; j < lm.get(i).size(); j++){
                    System.out.println("i = " + i);
                    System.out.println("j = " + j);
                            
                    if(lm.get(i).get(j) == value){
                        posArray[i][0] = i;
                        posArray[i][1] = j;
                        System.out.println("the value at row " + i + " column " + j + " is " + value);
                        System.out.println();
                    }
                }
            }
            for(int i = 0; i < posArray.length; i++){
                for(int j = 0; j < posArray[i].length; j++){
                    System.out.print(posArray[i][j] + " ");
                }
                System.out.println();
            }
            return posArray;
        }
    Which returns the following:

    Java Code:
    this is time # 0 thru the inner loop.
    
    i = 0
    j = 0
    the value at row 0 column 0 is false
    
    i = 0
    j = 1
    the value at row 0 column 1 is false
    
    i = 0
    j = 2
    the value at row 0 column 2 is false
    
    this is time # 1 thru the inner loop.
    
    i = 1
    j = 0
    the value at row 1 column 0 is false
    
    i = 1
    j = 1
    the value at row 1 column 1 is false
    
    i = 1
    j = 2
    the value at row 1 column 2 is false
    
    this is time # 2 thru the inner loop.
    
    i = 2
    j = 0
    the value at row 2 column 0 is false
    
    i = 2
    j = 1
    the value at row 2 column 1 is false
    
    i = 2
    j = 2
    the value at row 2 column 2 is false
    
    0 2 
    1 2 
    2 2 
    0 0 
    0 0 
    0 0 
    0 0 
    0 0 
    0 0
    I'm still thinking about this and remain hopeful. It's something to do with the
    Java Code:
     if(lm.get(i).get(j) == value){
                        posArray[i][0] = i;
                        posArray[i][1] = j;
                        System.out.println("the value at row " + i + " column " + j + " is " + value);
                        System.out.println();
                    }
    statement, I think.

    The getPositions(value) method is supposed to return an n x 2 array of ints, which correspond to the coordinates of the values in the matrix. So the expected output for a 3x3 matrix with 9 false values would look like:
    00
    01
    02
    10
    11
    12
    20
    21
    22
    Last edited by whimsical; 04-23-2012 at 04:21 AM. Reason: Clarifying what the method is supposed to return.

  8. #8
    whimsical is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Trouble with 2d ArrayList

    HOORAY! I put a counter in there and it worked.

    Java Code:
     public int[][] getPositions(boolean value) {
            int numValues = 0;
            int numValueCounter = 0;
            int[][] posArray = null;
            if(value == true){
                posArray = new int[getNumberTrueValues()][2];
                numValues = getNumberTrueValues();
            }
            else if(value == false){
                posArray = new int[getNumberFalseValues()][2];
                numValues = getNumberFalseValues();
                
            }
            for(int i = 0; i < lm.size(); i++){
                //System.out.println("this is time # " + i + " thru the inner loop.");
                //System.out.println();
                for(int j = 0; j < lm.get(i).size(); j++){
                    //System.out.println("i = " + i);
                    //System.out.println("j = " + j);
                            
                    if(lm.get(i).get(j) == value){
                        posArray[numValueCounter][0] = i;
                        posArray[numValueCounter][1] = j;
                        numValueCounter++;
                        //System.out.println("numValueCounter = " + numValueCounter);
                        //System.out.println("the value at row " + numValueCounter + " column " + j + " is " + value);
                        //System.out.println();
                    }
                }
            }
            for(int i = 0; i < posArray.length; i++){
                for(int j = 0; j < posArray[i].length; j++){
                    System.out.print(posArray[i][j] + " ");
                }
                System.out.println();
            }
            return posArray;
        }

Similar Threads

  1. trouble with ArrayList
    By rippon in forum New To Java
    Replies: 3
    Last Post: 09-30-2011, 01:18 AM
  2. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  3. arraylist trouble
    By Reiyn in forum New To Java
    Replies: 9
    Last Post: 05-28-2009, 12:03 AM
  4. Trouble with ArrayList
    By John D. in forum New To Java
    Replies: 4
    Last Post: 03-18-2009, 08:43 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 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
  •