Velocity Reviews > Java > 2D-Arrays:Keep columns, while changing all other values

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

John Inge =?UTF-8?B?RWxsaW5nc8OmdGVy?=
Guest
Posts: n/a

 03-20-2006

How do I do this??

I have a table [i][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]+" - ");
}
}

Dirk Michaelsen
Guest
Posts: n/a

 03-20-2006
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

Roedy Green
Guest
Posts: n/a

 03-20-2006
On Mon, 20 Mar 2006 12:26:03 +0100, John Inge Ellingsæter
<(E-Mail Removed)> 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.

--
http://mindprod.com Java custom programming, consulting and coaching.