# Traversing 2D Java Array

Page 1 of 1

## 8 Replies - 18736 Views - Last Post: 08 April 2009 - 06:38 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=97644&amp;s=dd4521911e079e7408729d67bf66eb1f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JDew

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 06-April 09

# Traversing 2D Java Array

Posted 06 April 2009 - 12:55 PM

Hi everyone,

I'm trying to implement the Robinson-Schensted Algorithm:

Quote

Given a Young tableau T, to row insert x into T,

* Set R equal to the first row of T
* While R contains an element greater than x, do

* Let y be the smallest element of R greater than x.
* Replace y by x in R.
* Set x = y and set R equal to the next row down.

* Place x at the end of the row R and stop.

http://en.wikipedia....nsted_algorithm

I've used a 2D Java Array, and am trying to traverse it to fill in a new element in the correct position according to the algorithm.

Here is some code I've come up with:

``` int y = currentElement;

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

int z = tableaux[rowCount][i];

if (z < y) {

tableaux[rowCount][i+1] = y;

}

else {

tableaux[rowCount][i] = y;

}

}
```

Am I on the right lines? It's really confusing me :S

It doesnt seem to be working, what's wrong?

Sorry if the code is really bad, im still a beginner

Thanks!

Is This A Good Question/Topic? 0

## Replies To: Traversing 2D Java Array

### #2 markhazlett9

• Coding is a lifestyle

Reputation: 61
• Posts: 1,666
• Joined: 12-July 08

## Re: Traversing 2D Java Array

Posted 06 April 2009 - 01:05 PM

To traverse through a 2D array, you need a for loop for every single dimension in the array. So for a 2D array you need 2 for loops embedded and for 3 you need 3 etc..

it will look like this...

```for(int first = 0; first < firstDimension.length; first++)
{
for(int second =0; second < secondDimension.length(); second++)
{
}
}

```

Was This Post Helpful? 0

### #3 JDew

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 06-April 09

## Re: Traversing 2D Java Array

Posted 06 April 2009 - 01:11 PM

markhazlett9, on 6 Apr, 2009 - 12:05 PM, said:

To traverse through a 2D array, you need a for loop for every single dimension in the array. So for a 2D array you need 2 for loops embedded and for 3 you need 3 etc..

it will look like this...

```for(int first = 0; first < firstDimension.length; first++)
{
for(int second =0; second < secondDimension.length(); second++)
{
}
}

```

ah I seee, so the first For loop can represent the Row for example, and the second one represents each Column within the current Row?
Was This Post Helpful? 0

### #4 markhazlett9

• Coding is a lifestyle

Reputation: 61
• Posts: 1,666
• Joined: 12-July 08

## Re: Traversing 2D Java Array

Posted 06 April 2009 - 01:13 PM

JDew, on 6 Apr, 2009 - 12:11 PM, said:

markhazlett9, on 6 Apr, 2009 - 12:05 PM, said:

To traverse through a 2D array, you need a for loop for every single dimension in the array. So for a 2D array you need 2 for loops embedded and for 3 you need 3 etc..

it will look like this...

```for(int first = 0; first < firstDimension.length; first++)
{
for(int second =0; second < secondDimension.length(); second++)
{
}
}

```

ah I seee, so the first For loop can represent the Row for example, and the second one represents each Column within the current Row?

EXACTLY! that's exactly what it is. Cheers
Was This Post Helpful? 0

### #5 JDew

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 06-April 09

## Re: Traversing 2D Java Array

Posted 07 April 2009 - 04:36 PM

Thank's for that, it was really helpful!

I have another problem now,

initially I declare the array of size [20][20], but what is the best way of keeping a count of the total number Row's that actually contain elements???

For example, say there is data in the columns of row 1-3 only, how do I keep a count as 3 and not 20
Was This Post Helpful? 0

### #6 markhazlett9

• Coding is a lifestyle

Reputation: 61
• Posts: 1,666
• Joined: 12-July 08

## Re: Traversing 2D Java Array

Posted 07 April 2009 - 05:14 PM

What you're dealing with there is what's called a ragged array. So what you would have to do is an if statement,

if(array[].length() > 0)

if you actually want to keep a counter of which ones contain the you will have to use another embedded for loop and keep an array of integers which size is equal to that of the original array size but that is much more complicated.
Was This Post Helpful? 0

### #7 JDew

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 06-April 09

## Re: Traversing 2D Java Array

Posted 07 April 2009 - 08:28 PM

markhazlett9, on 7 Apr, 2009 - 04:14 PM, said:

What you're dealing with there is what's called a ragged array. So what you would have to do is an if statement,

if(array[].length() > 0)

if you actually want to keep a counter of which ones contain the you will have to use another embedded for loop and keep an array of integers which size is equal to that of the original array size but that is much more complicated.

Thanks again for your help,

I think I will probably need to use this more complicated method due to the way I must fill the array.

For example:

Say I have 18 as the first element [0][0]

18

Then I enter the next element of 16, 18 would shift under 16

16

18

then if I entered say 24, it would be added to the right of 16

16 24

18

then if I entered 26 for example, it would need to be placed to the right of 24

16 24 26

18

But to find the correct cell for 26 (ie. next one after 14's cell) I would need to know the position of 24 right?

Or is there a simpler way this can be achieved, I've been trying with some While-For-If loops but not getting this exact result so far

This post has been edited by JDew: 07 April 2009 - 08:32 PM

Was This Post Helpful? 0

### #8 markhazlett9

• Coding is a lifestyle

Reputation: 61
• Posts: 1,666
• Joined: 12-July 08

## Re: Traversing 2D Java Array

Posted 07 April 2009 - 09:20 PM

Basically what you can do is go

int item24 = array[0][2];

That would get the item at row 0 column 2. Then if you try and get an item that is not there then you'll get a null pointer exception.

Cheers
Was This Post Helpful? 0

### #9 JDew

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 06-April 09

## Re: Traversing 2D Java Array

Posted 08 April 2009 - 06:38 AM

markhazlett9, on 7 Apr, 2009 - 08:20 PM, said:

Basically what you can do is go

int item24 = array[0][2];

That would get the item at row 0 column 2. Then if you try and get an item that is not there then you'll get a null pointer exception.

Cheers

Coool, thanks i'll give that a try
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }