Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
John Inge Ellingsæter - 20 Mar 2006 12:26 GMT
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.

Thanks in advance,
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++) {
       radNr = radNr+1;
       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 - 20 Mar 2006 13:38 GMT
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 - 20 Mar 2006 22:52 GMT
On Mon, 20 Mar 2006 12:26:03 +0100, John Inge Ellingsæter
<johellin@stop.online.no> 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.

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.