Trying to read a list of numbers in from the arraylist, and stick em into a matrix of int[][]

int[][] board;
board = new int[row][col];
ArrayList<Integer> temp = new ArrayList<Integer>();

so those are my two variables, and i have a for loop with variables, i and j...so I am trying to put the values of temp, into board, and it's not liking this method..

board = board[temp.get(i)][temp.get(j)];

^ why does that not work....or how can I get it to work...thanks.
Warning I get:
http://imageshack.us/photo/my-images/535/codeln.png/
-Austin

Recommended Answers

All 33 Replies

Have you worked out where the elements of the arraylist (single dim) are to go in the two dim array?
For example, arraylist index on left, board indexes on right:
0 -> 0,0
1 -> 0,1
etc for all the elements in the arraylist.

When you write out enough of the above list, you should see the pattern for how the indexes need to change to do the copying.

Well, I can't even do that part yet, because the environment wont accept the board = board[temp.get(i)][temp.get(j)]; Giving errors about int and int[][] being incompatible.

I can't even do that part yet

You're trying to write code before you have worked out the logic.
You need to work out the logic BEFORE writing the code.
Why are you using the contents of the arraylist as indexes into the array?

Part of my code...

public static int[][] load(Scanner scanner) {
		ArrayList<Integer> temp = new ArrayList<Integer>();
		
		int row = 0, col = 0;

		while (scanner.hasNextLine()) {
			temp.add(scanner.nextInt());
		}
		row = temp.get(0);
		col = temp.get(1);
		int[][] board;
		board = new int[row][col];

		temp.remove(0);
		temp.remove(0);
		if ((row * col) != temp.size()) {
			System.out.println("Invalid Matrix Size");
			System.exit(1);
		} else {
			
			for (int i = 0; i < row; i++) {
				
				for (int j = 0; j < col; j++) {
					
					board[row][col] = board[temp.get(i)][temp.get(j)];
					
				}

			}

		}
		

		
		
		return board;

	}

I am doing the calling, because the ArrayList already has the values within itself.

You need to work out the logic BEFORE writing the code.
List the assignments that need to be done to do the job. Something like this:


Have you worked out where the elements of the arraylist (single dim) are to go in the two dim array?
For example, arraylist index on left, board indexes on right:
0 -> 0,0
1 -> 0,1
2 -> 0,2
etc for all the elements in the arraylist.

Shouldn't what I have work though....it runs the first loop, then immediately goes into the 2nd loop and keeps running that one, till the col is filed. Then i goes back into the first loop for the second value, then goes to the col loop and does that one again till its done, ect...

OK, you are doing something wrong in this case. What do you think your ArrayList is containing after reading in? I understand that the first and second values are the row and column number. Now what other numbers in the ArrayList? Can you show us the sample data you attempt to read in?

I am guessing that the number you read is as follow...

/*
row_number
column_number
int
int
int

For example, a data in a file is as followed...
3  <- 3 rows
4  <- 4 columns
4  <- row 1, column 1
6  <- row 1, column 2
0  <- row 1, column 3
4  <- row 1, column 4
1  <- row 2, column 1
6  <- row 2, column 2
7  <- row 2, column 3
2  <- row 2, column 4
4  <- row 3, column 1
2  <- row 3, column 2
0  <- row 3, column 3
9  <- row 3, column 4
*/

You are using the assignment wrong in this case. The "board" variable is now a 2D array. In order to assign a value in it, you should do...

for (int i=0; i<row; i++) {
  for (int j=0; j<col; j++) {
    board[i][j] = temp.get(****);
  }
}

The **** is what you need to use in computation to get the correct position. You just need to look at how your data is added and use the current loop value to fill it in. I am sure it is not too difficult. :)

commented: spoon feeding -3

How is the OP to learn to solve his problems with people solving the problem for them?

Well I go this part fixed.

board[row][col] = board[temp.get(i)][temp.get(j)];

Sample data would be this scanned in..
"3 4 8 4 3 2 7 1 9 6 2 2 5 3"

With a return of: {{8, 4, 3, 2}, {7, 1, 9, 6}, {2, 2, 5, 3}}

I thought my loop would have covered all the values...maybe I am thinking about this wrong?

On the side, I know it is not working because these aren't even printing out at all..
System.out.println(board[0][0]);
System.out.println(board[0][1]);
System.out.println(board[0][2]);

these aren't even printing out at all..

If nothing is printed That would mean that they are not being executed.
What in your logic could keep those statements from executing?

Well, I do not see what would be stopping them, as the code fully executes through...

public static int[][] load(Scanner scanner) {
		ArrayList<Integer> temp = new ArrayList<Integer>();
		
		int row = 0, col = 0;

		while (scanner.hasNextLine()) {
			temp.add(scanner.nextInt());
		}
		row = temp.get(0);
		col = temp.get(1);
		int[][] board;
		board = new int[row][col];
		
		temp.remove(0);
		temp.remove(0);
		if ((row * col) != temp.size()) {
			System.out.println("Invalid Matrix Size");
			System.exit(1);
		} else {
			
			
			for (int i = 0; i < row; i++) {
				
				for (int j = 0; j < col; j++) {
					
					board[row][col] = board[temp.get(i)][temp.get(j)];
					
				}

			}
			
			

		}
		
			
			System.out.println(board[0][0]);
			System.out.println(board[0][1]);
			System.out.println(board[0][2]);
		
		
		

		
		
		return board;

	}

Ok, so I think I am getting confused about what is actually being stored WITHIN the int[][], and what is changing the value of the size of the int[][], can anyone clear it up for me a little bit please?

You don't change the size of an existing array. If you need to have an array of a different size, you create a new one.

int[][] board = new int[row][col];  // Create an array of rows and cols
...
board[row][col] = <THE VALUE TO ASSIGN TO THIS ELEMENT>; // store value in array

Read in the Tutorial:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Ok, so I was on the right track with that, but with the board[row][col] = I must have always something within a [][] format, right?

If so, then shouldn't the println's I asked to print at least be showing something..? Even if it is just 0,0?

I see two ways out of the method. System.exit() and return.
What shows on the console when you execute the program?
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Well, I run the TestCases given to us, then that runs into my class, and the Console displays nothing at all..

Which is why I am lost. lol, no errors are given, so I assume it runs all the way return statement, which then goes to the last class which is empty right now. As for the System.exit(), that won't apply right now as I am running all valid test cases, you can see it manually within the test case I posted as well.

String given: 3 4 8 4 3 2 7 1 9 6 2 2 5 3
3 * 4 are the rows n cols.

Result that board should hold after it's done running my method:
{{8, 4, 3, 2}, {7, 1, 9, 6}, {2, 2, 5, 3}}

Since the 3 *4 = 12, which is the number of integers in the returned array, the exit command won't even go

EDIT: I am using Eclipse IDE btw

Oh, wait a minute....it is terminating...


I shall look into it right now, but here is the screenie. BRB
http://imageshack.us/photo/my-images/695/code1q.png/

EDIT: So it is outofbounds...but I also posted some test lines in the for loop as follows

for (int i = 0; i < row; i++) {

				for (int j = 0; j < col; j++) {
					System.out.println("0,0:  " + board[0][0]);
					System.out.println("0,1:  " + board[0][1]);
					System.out.println("0,2:  " + board[0][2]);
					System.out.println("1,0:  " + board[1][0]);
					System.out.println("1,1:  " + board[1][1]);
					System.out.println("1,2:  " + board[1][2]);
					board[row][col] = board[temp.get(i)][temp.get(j)];

				}

			}

With the output of: (FYI, it is showing 3 loops, since there are 3 test cases running, I only posted the first one, but I can post the other 2 if you wish)

0,0: 0
0,1: 0
0,2: 0
1,0: 0
1,1: 0
1,2: 0
0,0: 0
0,1: 0
0,2: 0
1,0: 0
1,1: 0
1,2: 0
0,0: 0
0,1: 0
0,2: 0
1,0: 0
1,1: 0
1,2: 0

I am trying to figure out where the 3 is coming from, for the outofbounds exception...let alone why it is throwing that error, but I am kind of stumped... :(

I CAN post the TestClass made by the prof if that would help you explain to me what is going on, because it is decently complex...for our level of knowledge he said. I really want to get this done and understand it, since arrays are a big foundation of JAVA itself.

board[row][col] = board[temp.get(i)][temp.get(j)];

You need to look at this code.
What do you intend that it do?

You still have not thought about how the code is supposed to work
Where are the numbers you want to put in the array?
How do you get the numbers one by one to put into the array?
Where in the array are each of the numbers to go?

Well, from what I have written, I BELIEVE, that once i runs once it will be set at 0, thust making temp.get(i) = temp.get(0), or the first value of the AL. Now, the second for loop will start running and will up temp.get(j) for 0,1,2,3,ect.. till it hits the < col amount.
During the col loop's cycles, it will update board[row][col] = board[temp.get(i)][temp.get(j)];, so in the first run, it will be board[row][col] = board[temp.get(0)][temp.get(1,2,3,4,ect..)];

Which....now does seem like something is wrong... how is board[row][col] being updated from board[value][value] in the first place?

if row|col = row location, col location, then where is the actual value being updated at, or stored? Am I only just getting the location of the array without adding anything?

How is the OP to learn to solve his problems with people solving the problem for them?

Please reread my post. I didn't give the solution to his problem but the "correct" syntax. How on earth a beginner knows exactly the syntax should be? Don't you read what he has been posting and kept using the wrong syntax all the time? To teach someone, I don't "assume" that the person knows what I know.

I guess I'm confusing you by asking more than one question at a time.
Let's do it one thing at a time.

Where is the first value that you want to assign to the array?
Write down the expression or method call that will get the first value to be assigned to the array. Nothing more, just the code to get the first value to assign to the array.

Ok, if my test string is 3 4 8 4 3 2 7 1 9 6 2 2 5 3
then I want 8 in board[0][0], then 4 in board[0][1], ect..

After I store the col and row, I remove those 2 values from the AL thus bumping the AL values down 2, so that the new AL is really:
8 4 3 2 7 1 9 6 2 2 5 3

So... WAIT! would that be board[j] = temp.get(0); which would be 8?? Or is that along the right path?

UPDATED CODE: (counter is initialized at 0 at the beginning of method.

for (int i = 0; i < row; i++) {

				for (int j = 0; j < col; j++) {
					System.out.println("0,0:  " + board[0][0]);
					System.out.println("0,1:  " + board[0][1]);
					System.out.println("0,2:  " + board[0][2]);
					System.out.println("1,0:  " + board[1][0]);
					System.out.println("1,1:  " + board[1][1]);
					System.out.println("1,2:  " + board[1][2]);
					board[i][j] = temp.get(counter);
					counter++;

				}

			}

See? That's the right track of the syntax you are beating around the bush...

Mhm!!! :) So I just got a crap ton of output this time from that...

0,0: 0
I removed this massive list of numbers because it was a blatant waste of space.

I shall try to see what is doing now really quick and edit this within 2 mins probably.

EDIT:
It looks like it is adding them, is there a way I can just print out the whole AL? Like how the expected return should look like? {{8, 4, 3, 2}, {7, 1, 9, 6}, {2, 2, 5, 3}}

WOOHOO!!!!! I think I did it with that code!!
I also just made this code to print out what I got stored into board!!! :D

for (int z = 0; z <row; z++) {
			for (int y = 0; y < col; y++) {
				System.out.print(board[z][y] + " ");
			}
			System.out.println();
		}
		System.out.println(board);

which returns

8 4 3 2
7 1 9 6
2 2 5 3
[[I@444cee32
8 4 0 2
7 1 9 0
2 2 5 3
[[I@7b112783
8 4 -1 2
7 1 9 -12
2 2 -5 3
[[I@23394894

for the test cases:

@org.junit.Test
   public void testLoadScanner_plain() throws Exception {
      testLoadScanner(new int[][]{{8, 4, 3, 2}, {7, 1, 9, 6}, {2, 2, 5, 3}}, "3 4 8 4 3 2 7 1 9 6 2 2 5 3");
   }

   @Test
   public void testLoadScanner_withZero() throws Exception {
      testLoadScanner(new int[][]{{8, 4, 0, 2}, {7, 1, 9, 0}, {2, 2, 5, 3}}, "3 4 8 4 0 2 7 1 9 0 2 2 5 3");
   }

   @Test
   public void testLoadScanner_withNegative() throws Exception {
      testLoadScanner(new int[][]{{8, 4, -1, 2}, {7, 1, 9, -12}, {2, 2, -5, 3}}, "3 4 8 4 -1 2 7 1 9 -12 2 2 -5 3");
   }

My next question is though, will
return board;

Show my output the way the test case wants it? I think it does because the Junit Test has a green check mark next to its test when I compiled it.

So I think for my next question you may need to actually quick compile the testcase file with mine to see what it is doing...There is a lot of stuff going on, and I am not sure what the next class is even requiring me to do..

I attached MY class MatrixLoader, and the testcase class we were given, MatrixLoaderTest.

I think that this part deals with actual text file input, so I attached two of those as well...

I am also taking a break from this for tonight, as I have to do some other homework, but I will be back at this post tomorrow bright and early.

Thank you both for the help thus far!! :)

Bump.

I am now back on this thread for a few hours if anyone wants to pipe in and help. :)

Please explain your current problem.

Well actually I think I figured it out. :)
By using...

public static int[][] load(String filename) {
		String line = "";
		File file = new File(filename);

		try {

			Scanner scanner = new Scanner(file);
			while (scanner.hasNextLine()) {
				line = scanner.nextLine();
				System.out.println(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		System.out.println("file scanned " + filename);

		return load(new Scanner(line));

	}

Maybe you can help me figure out how to return a "throw" or "exception" though for the test cases, rather than System.exit(1)?

As you can see from the JUnit test, everything goes through, however the last 2 tests (the file-based one) are not necessarily a "green check mark" like the top 3.

Screenshot: http://imageshack.us/photo/my-images/853/testsb.png/

Sorry, I'm not familiar with JUnit test.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.