method for 2D arrays

how to create a method for printing 2D array?

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 6260 Views - Last Post: 16 July 2010 - 11:59 AM Rate Topic: -----

#1 eugene3foo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-June 10

method for 2D arrays

Posted 16 July 2010 - 11:04 AM

I have been trying to get this method to work however i could not complie it.
My sntax could be wrong since it is my 1st time doing a method.
could you advice me on what mistakes i made Many thanks!

public class methodfor2Darray
{
	public static void main (String [] args)
	{
		int [] [] numbersone = {1,2,3,4,5}, numbertwo = {1,2,3,4,5};
		TableArray(numbersone, numbertwo);
	}
	public static void TableArray ( int [] [] x, y)
	{
		for (int i = 0; i < x.length; i++)
		{
			for (int r = 0; r < y.length; r++)
			System.out.print( x, y [i][r] + " ");
		}
		System.out.println();
	}
}



this is the following error i get
methodfor2Darray.java:8: <identifier> expected
	public static void TableArray ( int [] [] x, y)
	                                              ^
1 error


Is This A Good Question/Topic? 0
  • +

Replies To: method for 2D arrays

#2 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: method for 2D arrays

Posted 16 July 2010 - 11:06 AM

You are not giving the parameter y a type. You must do this in the method declaration.

Is it an int? A double? etc...

Appears to me like it should be an array.

This post has been edited by eZACKe: 16 July 2010 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

#3 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: method for 2D arrays

Posted 16 July 2010 - 11:06 AM

edit,

yea man just declare the other one as a 2d array as well. may i ask why you need a 2d?

This post has been edited by irmoathlete: 16 July 2010 - 11:10 AM

Was This Post Helpful? 0
  • +
  • -

#4 eugene3foo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-June 10

Re: method for 2D arrays

Posted 16 July 2010 - 11:11 AM

hmmm... i m still confused
 public static void TableArray ( int [] [] x, y)  

this is a 2D array or a single array??
Was This Post Helpful? 0
  • +
  • -

#5 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: method for 2D arrays

Posted 16 July 2010 - 11:13 AM

x is a 2D array. Can't tell what y is though because there is no type.

The comma separates parameters. You have two different parameters for this method: x, and y.

This post has been edited by eZACKe: 16 July 2010 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#6 eugene3foo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-June 10

Re: method for 2D arrays

Posted 16 July 2010 - 11:13 AM

erm i just interested in JAVA and learn it on my own so i just want to know how to do this
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10184
  • View blog
  • Posts: 37,599
  • Joined: 27-December 08

Re: method for 2D arrays

Posted 16 July 2010 - 11:14 AM

Unlike with instance and global variables where you can use the shorthand: int x,y,z;, you have to explicitly declare type for each parameter in a method definition. So more like: int[][] x, int[][] y.
Was This Post Helpful? 1
  • +
  • -

#8 eugene3foo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-June 10

Re: method for 2D arrays

Posted 16 July 2010 - 11:17 AM

View PosteZACKe, on 16 July 2010 - 10:13 AM, said:

x is a 2D array. Can't tell what y is though because there is no type.



Oh. but how do i giv in the elements for the rows if i do not hav 2 different int?? and what u mean is this??
public static void TableArray ( int [] [] x)

this itself is a 2D array then how do i plot the elements?
Was This Post Helpful? 0
  • +
  • -

#9 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: method for 2D arrays

Posted 16 July 2010 - 11:20 AM

guys, when he prints (x,y[i][r]) wouldnt that just give him whatever x is and then print the y 2d array after he declares it?

wouldn't you want to do something like this:


System.out.println(x[i][r]+y[i][r]);



??

basically your first [] is rows and your second [] is columns. correct me if i'm wrong please. when you access it as x[i][r] in your nested loops your calling i-row and r-column
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10184
  • View blog
  • Posts: 37,599
  • Joined: 27-December 08

Re: method for 2D arrays

Posted 16 July 2010 - 11:22 AM

View Posteugene3foo, on 16 July 2010 - 02:17 PM, said:

View PosteZACKe, on 16 July 2010 - 10:13 AM, said:

x is a 2D array. Can't tell what y is though because there is no type.



Oh. but how do i giv in the elements for the rows if i do not hav 2 different int?? and what u mean is this??
public static void TableArray ( int [] [] x)

this itself is a 2D array then how do i plot the elements?

Actually, it isn't a 2D array. It only points to one in memory. When you pass a 2D array variable to this method, the parameter references the array, allowing the method to work with it. Whenever you accept params in a method, it isn't the method's responsibilities to initialize them. It is assumed that this has been done. So you should focus on writing the code for processing the 2D array.
Was This Post Helpful? 0
  • +
  • -

#11 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: method for 2D arrays

Posted 16 July 2010 - 11:23 AM

Well eugene3foo, you are not using 2D arrays in this really. You should be getting errors beyond just the one in the method declaration.

You are saying:
int [] [] numbersone = {1,2,3,4,5}, numbertwo = {1,2,3,4,5};



You are saying numbersone and numbertwo are suppose to both be 2D arrays, yet you only assign them as if they are just single arrays.

You might want to do something like this?
int [] [] numbersone = {{1,2,3,4,5},{6,7,8,9,10}} numbertwo = {{1,2,3,4,5},{6,7,8,9,10}};



Those are 2D arrays. Otherwise, just use single arrays. Simply just do:
int [] numbersone = {1,2,3,4,5}, numbertwo = {1,2,3,4,5};


Was This Post Helpful? 0
  • +
  • -

#12 eugene3foo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-June 10

Re: method for 2D arrays

Posted 16 July 2010 - 11:24 AM

yes but how do i plot the eleents after that i had changed my code to this
 public class Q2_PRSP2Ver2
{
	public static void main (String [] args)
	{
		int [] [] numbersone = {1,2,3,4,5}, {1,2,3,4,5};
		TableArray(numbersone);
	}
	public static void TableArray ( int [] [] x)
	{
		for (int i = 0; i < x.length; i++)
		{
			for (int r = 0; r < y.length; r++)
			System.out.print( x[i][r] + " ");
		}
		System.out.println();
	}
}


but more error comes out
Q2_PRSP2Ver2.java:5: <identifier> expected
		int [] [] numbersone = {1,2,3,4,5}, {1,2,3,4,5};
		                                   ^
Q2_PRSP2Ver2.java:5: not a statement
		int [] [] numbersone = {1,2,3,4,5}, {1,2,3,4,5};
		                                     ^
Q2_PRSP2Ver2.java:5: ';' expected
		int [] [] numbersone = {1,2,3,4,5}, {1,2,3,4,5};
		                                      ^
Q2_PRSP2Ver2.java:6: invalid method declaration; return type required
		TableArray(numbersone);
		^
Q2_PRSP2Ver2.java:6: <identifier> expected
		TableArray(numbersone);
		                     ^
Q2_PRSP2Ver2.java:8: class, interface, or enum expected
	public static void TableArray ( int [] [] x)
	              ^
Q2_PRSP2Ver2.java:10: class, interface, or enum expected
		for (int i = 0; i < x.length; i++)
		                ^
Q2_PRSP2Ver2.java:10: class, interface, or enum expected
		for (int i = 0; i < x.length; i++)
		                              ^
Q2_PRSP2Ver2.java:12: class, interface, or enum expected
			for (int r = 0; r < y.length; r++)
			                ^
Q2_PRSP2Ver2.java:12: class, interface, or enum expected
			for (int r = 0; r < y.length; r++)
			                              ^
Q2_PRSP2Ver2.java:14: class, interface, or enum expected
		}
		^
Q2_PRSP2Ver2.java:16: class, interface, or enum expected
	}
	^
12 errors



I just need to print one 2D array using a method.
Was This Post Helpful? 0
  • +
  • -

#13 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: method for 2D arrays

Posted 16 July 2010 - 11:26 AM

Try this then:
int [] [] numbersone = {{1,2,3,4,5},{1,2,3,4,5}};



That's how you initialize a 2D array.

This post has been edited by eZACKe: 16 July 2010 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#14 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: method for 2D arrays

Posted 16 July 2010 - 11:27 AM

You have to do what Ezack said and do the
	int [] [] numbersone = {{1,2,3,4,5},{6,7,8,9,10}}



This post has been edited by irmoathlete: 16 July 2010 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#15 eugene3foo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 04-June 10

Re: method for 2D arrays

Posted 16 July 2010 - 11:28 AM

i had changed to
int [] [] numbersone = {{1,2,3,4,5}, {1,2,3,4,5}};


but the error is this
Q2_PRSP2Ver2.java:12: cannot find symbol
symbol  : variable y
location: class Q2_PRSP2Ver2
			for (int r = 0; r < y.length; r++)
			                    ^
1 error


what should i put for that?

This post has been edited by eugene3foo: 16 July 2010 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2