Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Java ArrayList to Matrix Conversion

    Be forewarned that this is a homework assignment and that I'm not asking for someone to do it for me, only for a little help since I can't figure out what the heck is wrong with it.

    In a nutshell, this program is supposed to take a one dimensional array list and make a matrix out it (multi-dimensional array). It may be my equation, stored under the variable of position, but I'm pretty sure that that is right because I checked it on paper. The problem is that it is outputting the matrix with the opposite dimensions of what I send as a parameter to the method. Instead of 3 columns and 5 rows, it prints out 5 columns and 3 rows. Thanks to anyone that can provide some insight into this mess.
    PHP Code:
    import java.util.ArrayList;

    public class 
    Lab26a8{
        public static 
    void main(String args[]){
            
    System.out.println("\nLAB26A 80-POINT VERSION\n");

            
    Matrix m2 = new Matrix(3,5);
            
    m2.displayMatrix("Matrix m2 3 X 5 Display");
            
    System.out.println();
            
    int count 100;
        }
    }

    class 
    Matrix{                    
        private 
    ArrayList list;  // one-dimensional array stores matrix values
        
    private int listSize;    // total number of elements in the matrix
        
    private int numRows;     // number of rows in the matrix
        
    private int numCols;     // number of cols in the matrix

        
    public Matrix(int cint r){
            list = new 
    ArrayList();
            
    numCols c;
            
    numRows r;
            
    listSize r;
            
            for(
    int i 0listSizei++)
                list.
    add(new Integer(0));
        }

        public 
    int getRows(){
            return 
    numRows;
        }
        
        public 
    int getCols(){
            return 
    numCols;
        }
        
        public 
    int getSize(){
            return 
    listSize;
        }
        
        public 
    int getValue(int cint r){
            
    int rowLength getRows();
            
    int colLength getCols();
            
            
    int position = ((1) * rowLength) - rowLength c;  // one to multi-dimensional array index conversion
            
    return ((Integer)list.get(position)).intValue();
        }
        
        public 
    void setValue(int rint cint value){
            
    int rowLength getRows();
            
    int colLength getCols();
            
            
    int position = ((1) * rowLength) - rowLength c;  // one to multi-dimensional array index conversion
            
            
    list.add(position,new Integer(value));
        }
        
        public 
    void displayMatrix(String str){
            
            
    System.out.println(str);
            
            for(
    int j 0getCols(); j++){
                for(
    int i 0getRows(); i++){
                    
    System.out.print(getValue(j,i) + " ");
                }
                
    System.out.println("");
            }
        }


  • #2
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, simple problem. Just switch your display for loop so that your loop is based on rows first, outside loop, and then columns, inner loop.

  • #3
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also, when you say a 3 x 5 matrix, that is actually a 3 row by 5 column matrix, which is not what you are outputting. I think it would make sense to reverse the order in which you do the program. Do rows calculations first, then column. For instance, you take columns first in the constructor. Hope I helped, although I did very little.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Thanks for the help, I thought that I would never get any lol. There are no runtime errors now, only logic errors. I think that the problem lies in the passing of the parameters as you stated, now I just have to figure out what in the heck is going on with them.

    Thanks again for the help man, you're a life-saver.

  • #5
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The code you initially posted had problems with the arraylist. It was getting out of bounds exceptions. I just through in some println's to check your position variable and notice it was going too far. Then it worked, but it did everything backwards, well kinda. It was actually producing the 3 x 5 matrix, but you really want a 5 x 3, so then I switched the loop construct and it worked perfect.

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Another problem has arisen and this one is pretty weird. I only showed you part of the code so that it would be easier on both of us to fix. Here's the same program, but with a different constructor being used. The problem seems to be with the variable count, but I don't understand what's going on with it.
    PHP Code:
    import java.util.ArrayList;

    public class 
    Lab26a8{
        public static 
    void main(String args[]){
            
    System.out.println("\nLAB26A 80-POINT VERSION\n");

            
    Matrix m2 = new Matrix(3,5);
            
    m2.displayMatrix("Matrix m2 3 X 5 Display");
            
    System.out.println();
            
    int count 100;
            
            
    System.out.println("m2.getRows() = " m2.getRows());  //
            
    System.out.println("m2.getCols() = " m2.getCols());  //
            
    System.out.println("");
            
            for (
    int r 0m2.getRows(); r++)
            {
                
    System.out.println("r = " r);  //
                
                
    for (int c 0m2.getCols(); c++)
                {
                    
    m2.setValue(r,c,count);
                    
    count++;
                    
    System.out.println("c = " c);  //
                    
    System.out.println("count = " count);  //
                
    }
                
    System.out.println("");  //
            
    }
            
            
    m2.displayMatrix("Matrix m2 3 X 5 Consecutive Integers Display");
            
    System.out.println();
        }
    }

    class 
    Matrix{                    
        private 
    ArrayList list;  // one-dimensional array stores matrix values
        
    private int listSize;    // total number of elements in the matrix
        
    private int numRows;     // number of rows in the matrix
        
    private int numCols;     // number of cols in the matrix

        
    public Matrix(int rint cint value){
            list = new 
    ArrayList();
            
    numRows r;
            
    numCols c;
            
    listSize c;
            
            for(
    int i 0listSizei++)
                list.
    add(new Integer(value));
        }

        public 
    int getRows(){
            return 
    numRows;
        }
        
        public 
    int getCols(){
            return 
    numCols;
        }
        
        public 
    int getSize(){
            return 
    listSize;
        }
        
        public 
    int getValue(int rint c){
            
    int rowLength getRows();
            
    int colLength getCols();
            
            
    int position = ((1) * rowLength) - rowLength c;  // one to multi-dimensional array index conversion
            
    return ((Integer)list.get(position)).intValue();
        }
        
        public 
    void setValue(int rint cint value){
            
    int rowLength getRows();
            
    int colLength getCols();
            
            
    int position = ((1) * rowLength) - rowLength c;  // one to multi-dimensional array index conversion
            
            
    list.add(position,new Integer(value));
        }
        
        public 
    void displayMatrix(String str){
            
            
    System.out.println(str);
            
            for(
    int j 0getRows(); j++){
                for(
    int i 0getCols(); i++){
                    
    System.out.print(getValue(j,i) + " ");
                }
                
    System.out.println("");
            }
        }


  • #7
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It looks like count is the value that is being used to give a value to the data that you are entering into the matrix. Instead of a zero, you are using 100.

  • #8
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, and you are adding 1 to count all the time too. I'm loading your new code right now.

  • #9
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It seems to run fine for me. What is concerning you?

  • #10
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    My output for that object is
    100 101 102 105 106
    105 106 107 110 111
    110 111 112 113 114
    Are you getting that too? I have no idea why it won't just count up one at a time instead of jumping from 102 to 105.

  • #11
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, it's your set and get value methods, specifically the method by which you obtain the position number. It seems to work when you are using an n x n matrix, 2 x 2, 3 x 3, etc., but fails when you are using an n x m.

    I also got the same values as you. You have to look at the position. There is an easier way to add the values. Just add to the list based on how many times you've been through the loop. Easier calculation for you and it works the same.

    I'll get back to you tomorrow on the position problem. Later.

  • #12
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Couldn't resist solving it tonight.

    Here is the new position algorithm -

    int position = (r * colLength) + c;

    Frankly though, why don't you just use a multi-dimension array for this.

    Here's the code I have.

    PHP Code:
    import java.util.ArrayList;

    public class 
    Matrix {

        private 
    ArrayList list;  // one-dimensional array stores matrix values 
        
    private int listSize;    // total number of elements in the matrix 
        
    private int numRows;     // number of rows in the matrix 
        
    private int numCols;     // number of cols in the matrix 

        
    public Matrix(int rint cint value){ 
            list = new 
    ArrayList(); 
            
    numRows r
            
    numCols c
            
    listSize c
             
            
    //for(int i = 0; i < listSize; i++) 
             //   list.add(new Integer(value)); 
        


        public 
    int getRows(){ 
            return 
    numRows
        } 
         
        public 
    int getCols(){ 
            return 
    numCols
        } 
         
        public 
    int getSize(){ 
            return 
    listSize
        } 
         
        public 
    int getValue(int rint c){ 
            
    int rowLength getRows(); 
            
    int colLength getCols();
            
    //System.out.println("r in get = " + r);
            
    int position = (colLength) + c;
            
    //int position = ((r + 1) * rowLength) - rowLength + c;  // one to multi-dimensional array index conversion
            //System.out.print("Pos get = " + position + "; ");
            
    return ((Integer)list.get(position)).intValue(); 
        } 
         
        public 
    void setValue(int rint cint valueint pos){ 
            
    int rowLength getRows(); 
            
    //int colLength = getCols();        
            //int position = ((r + 1) * rowLength) - rowLength + c;  // one to multi-dimensional array index conversion 
            //System.out.println("Pos set = " + position + "; ");
            
    list.add(pos,new Integer(value)); 
        } 
         
        public 
    void displayMatrix(String str){ 
             
            
    System.out.println(str); 
             
            for(
    int j 0getRows(); j++){ 
                for(
    int i 0getCols(); i++){ 
                    
    System.out.print(getValue(ji) + " "); 
                } 
                
    System.out.println(""); 
            } 
        }
        
        public static 
    void main(String[] args) {
            
    System.out.println("\nLAB26A 80-POINT VERSION\n"); 

            
    Matrix m2 = new Matrix(35100); 
            
    //m2.displayMatrix("Matrix m2 3 X 5 Display"); 
            
    System.out.println(); 
            
    int count 100
             
            
    System.out.println("m2.getRows() = " m2.getRows());  // 
            
    System.out.println("m2.getCols() = " m2.getCols());  // 
            
    System.out.println("");
            
            
    int pos 0;
             
            for (
    int r 0m2.getRows(); r++) 
            { 
                
    //System.out.println("r = " + r);  // 
                 
                
    for (int c 0m2.getCols(); c++) 
                { 
                    
    m2.setValue(r,c,countpos);
                    
    pos++;
                    
    count++; 
                    
    //System.out.println("r = " + r + " c = " + c + " and count = " + count);  // 
                

                
    //System.out.println("");  // 
            

             
            
    m2.displayMatrix("Matrix m2 3 x 5 Consecutive Integers Display"); 
            
    //System.out.println(""); 

        
    }

    If you have any question, just fire them off.

  • #13
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    That works beautifully, thank you.

    Any idea why my algorithm wouldn't work? I realize now that it's overcomplicated, but it worked on paper. Oh, about using a multi-dimensional array, everyone else in class was doing that so I was trying to do it a different way.

  • #14
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I started doing the math on your algorithm and it just didn't work. It worked for the first row, but stopped working whenever you went to the second row, at least with an n x m matrix. I'm not sure how it worked for you, but it shouldn't have. Just a reminder, had you started your row and column counting at 1, the algorithm would be a little different.

    Basically, your algorithm just wasn't calculating exactly what it should have been calculating.

  • #15
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Alright, fair enough. Thanks again for your help, I'm deeply indebted to you. This flu has really gotten me behind in my school work and this has helped me a great deal.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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