Closed Thread Icon

Topic awaiting preservation: Java - two dimensional arrays (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=26806" title="Pages that link to Topic awaiting preservation: Java - two dimensional arrays (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Java - two dimensional arrays <span class="small">(Page 1 of 1)</span>\

 
KARN
Bipolar (III) Inmate

From: North Bay, Ontario, CA
Insane since: Apr 2001

posted posted 10-07-2005 04:34

hmmm don't know if this actually fits in here... question about actual java - two dimensional array... wondering why this gives me an out of bound error:

code:
public class Matrix
{
	
	private static int rows = 5;
	private static int cols = 8;
	
	public static void main(String[] args)
	{
		int[][] matrix = new int[rows][cols];
		
		int r,c;
		
		for (r=0; r < rows-1; r++)
			for (c=0; c < rows-1; c++)
				matrix[rows][cols] = 1;
	}
}



Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-07-2005 04:41

You should be using matrix[r][c] instead of [rows][cols].

Also, you probably intended to say "r < rows" instead of "r < rows - 1" or you'll miss the last element.

KARN
Bipolar (III) Inmate

From: North Bay, Ontario, CA
Insane since: Apr 2001

posted posted 10-07-2005 05:01

yeap thanks.. i changed it... now i was wondering.. how abouts do i go in outputting this?

_Mauro
Bipolar (III) Inmate

From:
Insane since: Jul 2005

posted posted 10-07-2005 09:24

I guess it is a console application, so you can
System.out.println(""+matrix[r][c]+"");

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 10-07-2005 09:26

and you should also do c < rows if you want to set all the rows.


well, same loop, and a bunch of System.WriteLine or whatever Java uses.

« BackwardsOnwards »

Show Forum Drop Down Menu