method for 2D arrays

how to create a method for printing 2D array?

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: method for 2D arrays

Posted 16 July 2010 - 11:29 AM

Did you declare y as a param or local variable within your method? Was it initialized?
Was This Post Helpful? 0
  • +
  • -

#17 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:29 AM

Well now you just need to take a second to step back and look at your code again. What is y? Did you not just get rid of that?

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

Was This Post Helpful? 0
  • +
  • -

#18 sh1n3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Re: method for 2D arrays

Posted 16 July 2010 - 11:31 AM

A more elaborate explanation is that the method signature :
[Access Modifier] [keywords] return-type Identifier(formal parameters) {
//Function Block
}


The '[' ']' show that you may or may not use those keywords if you want to. But the rest are compulsory.
return-type refers to the data being returned by a method -

ex: if method returns 3
return-type: int
ex: if method returns 3.0
return-type: double
ex: if method returns "sh1457"
return-type: String
ex: if method returns obj
return-type: Object

The Identifier is the name of the method, almost anything will do but look into java Code Conventions for the generally practiced way.

The formal parameters are copies of the the actual parameters that are sent when you call the method.
static int add(int a, int B)/> // Formal Parameters copies of x and y 
{
return (a+B)/>;
}

public static void main(String[] args) //Also Formal 
{
int x=5;
int y=6;
add(x,y); // Actual Parameters
// This type of calling is called call by reference
// and the actual values remain unchanged
}



Each of the Formal parameter's data type should be specified because when you call the method the sequence of the actual values get stored in the same sequence of formal parameters. Helps when you have parameters with more than one data type.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#19 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: method for 2D arrays

Posted 16 July 2010 - 11:41 AM

Then field y doesn't exist in your method.
I think you have to access the 2nd element in your method as x[].length (???? or something similar)
Was This Post Helpful? 0
  • +
  • -

#20 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:48 AM

ya i do unserstand that but how do i transverse the row part 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 < x.length; r++)
			System.out.print( x[i][r] + " ");
		}
		System.out.println();
	}
}


It prints 1 2 1 2.
ahhaha still rather confused sorry
Was This Post Helpful? 0
  • +
  • -

#21 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: method for 2D arrays

Posted 16 July 2010 - 11:50 AM

You'll want to use x[i].length to access the length of a nested array in your inner loop as x.length accesses the number of arrays in your 2D array.
Was This Post Helpful? 0
  • +
  • -

#22 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:52 AM

Try doing a little debugging, this is what I tried before figuring out exactly what needed to be done.
System.out.println(x.length);



That will show you that the length is 2. Which makes sense because there are two arrays within the 2D array. So the size is 2. Thus, if for the inner for loop you are still getting the overall length, it's only going to go through once.

So what that tells us is for the inner for loop we need to get the lengths of each individual array within the 2D array. Now how would we do that?
x[i].length



What does that give us? That gives the length or the individual element. The individual elements are the two arrays within the 2D array. Hooray!

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#23 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:57 AM

wouldnt you just want your for loops to do this?


for(...;x[i].length;...)
     for(...;x[r].length;...)





edit** dang people respond fast

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

Was This Post Helpful? 0
  • +
  • -

#24 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:58 AM

OH!!! i see thanks!!! :)

but is it suppose to print
1 2 3 4 5 1 2 3 4 5

i thought it suppose to print
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
or
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
Was This Post Helpful? 0
  • +
  • -

#25 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:59 AM

Now, how could you make it print in the correct format?

What about just throwing in a new line every time the inner for loop is done?

System.out.println();
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2