Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Java ArrayList to Matrix Conversion

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-02-2004, 04:31 AM PM User | #1 Antoniohawk 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 c, int r){         list = new ArrayList();         numCols = c;         numRows = r;         listSize = c * r;                  for(int i = 0; i < listSize; i++)             list.add(new Integer(0));     }     public int getRows(){         return numRows;     }          public int getCols(){         return numCols;     }          public int getSize(){         return listSize;     }          public int getValue(int c, int r){         int rowLength = getRows();         int colLength = getCols();                  int position = ((r + 1) * rowLength) - rowLength + c;  // one to multi-dimensional array index conversion         return ((Integer)list.get(position)).intValue();     }          public void setValue(int r, int c, int value){         int rowLength = getRows();         int colLength = getCols();                  int position = ((r + 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 = 0; j < getCols(); j++){             for(int i = 0; i < getRows(); i++){                 System.out.print(getValue(j,i) + " ");             }             System.out.println("");         }     } }  ``` __________________ xhtml :: attributes . tags . vs. CSS :: Box Model Hack [CF T-Shirts] [CP Resources] [ithium hosting] [A New Generation of Tree Menu]
 11-07-2004, 03:49 AM PM User | #2 turbowrx 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.
 11-07-2004, 03:54 AM PM User | #3 turbowrx 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.
 11-07-2004, 04:39 AM PM User | #4 Antoniohawk 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. __________________ xhtml :: attributes . tags . vs. CSS :: Box Model Hack [CF T-Shirts] [CP Resources] [ithium hosting] [A New Generation of Tree Menu]
 11-07-2004, 05:41 AM PM User | #5 turbowrx 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.
 11-07-2004, 05:48 AM PM User | #6 Antoniohawk 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 = 0; r < m2.getRows(); r++)         {             System.out.println("r = " + r);  //                          for (int c = 0; c < m2.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 r, int c, int value){         list = new ArrayList();         numRows = r;         numCols = c;         listSize = r * 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 r, int c){         int rowLength = getRows();         int colLength = getCols();                  int position = ((r + 1) * rowLength) - rowLength + c;  // one to multi-dimensional array index conversion         return ((Integer)list.get(position)).intValue();     }          public void setValue(int r, int c, int value){         int rowLength = getRows();         int colLength = getCols();                  int position = ((r + 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 = 0; j < getRows(); j++){             for(int i = 0; i < getCols(); i++){                 System.out.print(getValue(j,i) + " ");             }             System.out.println("");         }     } }  ``` __________________ xhtml :: attributes . tags . vs. CSS :: Box Model Hack [CF T-Shirts] [CP Resources] [ithium hosting] [A New Generation of Tree Menu]
 11-07-2004, 06:09 AM PM User | #7 turbowrx 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.
 11-07-2004, 06:10 AM PM User | #8 turbowrx 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.
 11-07-2004, 06:15 AM PM User | #9 turbowrx 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?
11-07-2004, 06:37 AM   PM User | #10
Antoniohawk
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
Quote:
 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-07-2004, 07:57 AM PM User | #11 turbowrx 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.
 11-07-2004, 08:17 AM PM User | #12 turbowrx 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 r, int c, int value){          list = new ArrayList();          numRows = r;          numCols = c;          listSize = r * 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 r, int c){          int rowLength = getRows();          int colLength = getCols();         //System.out.println("r in get = " + r);         int position = (r * 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 r, int c, int value, int 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 = 0; j < getRows(); j++){              for(int i = 0; i < getCols(); i++){                  System.out.print(getValue(j, i) + " ");              }              System.out.println("");          }      }          public static void main(String[] args) {         System.out.println("\nLAB26A 80-POINT VERSION\n");          Matrix m2 = new Matrix(3, 5, 100);          //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 = 0; r < m2.getRows(); r++)          {              //System.out.println("r = " + r);  //                            for (int c = 0; c < m2.getCols(); c++)              {                  m2.setValue(r,c,count, pos);                 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.
 11-07-2004, 05:34 PM PM User | #13 Antoniohawk 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. __________________ xhtml :: attributes . tags . vs. CSS :: Box Model Hack [CF T-Shirts] [CP Resources] [ithium hosting] [A New Generation of Tree Menu]
 11-07-2004, 07:32 PM PM User | #14 turbowrx 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.
 11-07-2004, 07:37 PM PM User | #15 Antoniohawk 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. __________________ xhtml :: attributes . tags . vs. CSS :: Box Model Hack [CF T-Shirts] [CP Resources] [ithium hosting] [A New Generation of Tree Menu]

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 08:39 PM.