# 2D-Arrays:Keep columns, while changing all other values

Discussion in 'Java' started by John Inge =?UTF-8?B?RWxsaW5nc8OmdGVy?=, Mar 20, 2006.

1. ### John Inge =?UTF-8?B?RWxsaW5nc8OmdGVy?=Guest

How do I do this??

I have a table [j] that are filled with random integers on each round of
a loop, but for a certain user input I want to keep column[n] while all
other numbers are changed.

I guess this is quite easy, but I just can't seem to figure it out.

John Inge E

If to any help, this is the code for filling the table with integers:
(some norwegian words in there, sorry about that)

for (int column = 0; column < antRader; column++) {
System.out.print("\t L" + radNr + " | ");

for (int row = 0; row < (antRader); row++) {
tabell[column][row] = ((int)(Math.random()*(antRandom)+1));
System.out.print(tabell[column][row]+" - ");
}
}

John Inge =?UTF-8?B?RWxsaW5nc8OmdGVy?=, Mar 20, 2006

2. ### Dirk MichaelsenGuest

Hi John Inge,

>I guess this is quite easy, but I just can't seem to figure it out.

yes it's very easy. Just test the column number before entering the
inner for-loop:

if (column != n) {
// put your inner loop here
}

cu
Dirk

Dirk Michaelsen, Mar 20, 2006

3. ### Roedy GreenGuest

On Mon, 20 Mar 2006 12:26:03 +0100, John Inge EllingsÃ¦ter
<> wrote, quoted or indirectly quoted someone
who said :

>tabell[column][row] = ((int)(Math.random()*(antRandom)+1));

there are much better ways of doing random numbers. See
http://mindprod.com/jgloss/pseudorandom.html

Your question amounts to how do you loop through just the elements you
want to change. If you can't solve that, try a loop that goes through
everything, and decided on a case by case basis whether to make the
change. Once you have that code working, your brain might clue into
how to code the efficient version. Just eliminate the wasteful parts
of the loop a bit at a time.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green, Mar 20, 2006