two dimensional arrays

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2007
Posts: 34
Reputation: baltazar is an unknown quantity at this point 
Solved Threads: 0
baltazar baltazar is offline Offline
Light Poster

two dimensional arrays

 
0
  #1
Jul 26th, 2007
Hi there,
What I am doing is filling a 2-D array with numbers in a random order.
i.e. I pick a position in the 2-D array and input a number there
eg: array[2][1] = 3;(this input is received from a text file)
In this way, I fill the array but some spots remain empty.
What I want to do is fill these empty position in the 2-D array with the number 99, but I don't know how to go through the array and test if each position is empty.
I tried going through the array and comparing each position to null but that didnt help, it gave me a "incomparible type" error.
Can someone help me?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,417
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 257
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: two dimensional arrays

 
0
  #2
Jul 26th, 2007
When you initiate the array (i.e. int[][] array = new int[20][20];) every position is already filled with 0 (since a primitive cannot be null). So, if zero is not one of the numbers you are using, simply cycle through the arrays with a nested loop replacing 0 with 99.
Last edited by masijade; Jul 26th, 2007 at 4:56 am. Reason: forgot a )
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,484
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: two dimensional arrays

 
0
  #3
Jul 26th, 2007
Yes, as masijade mentioned, initialize the array elements to the value you want to represent "empty" before you start filling it. You didn't say what the valid range for your number entries was, but filling with -1 or Integer.MIN_VALUE can be useful values to represent "unset".
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 2
Reputation: anamika_nagpur is an unknown quantity at this point 
Solved Threads: 1
anamika_nagpur anamika_nagpur is offline Offline
Newbie Poster

Re: two dimensional arrays

 
0
  #4
Aug 8th, 2007
Agree that a premitive cannot be null. But what if one of the position is actually filled with number zero [0] from a text file (zero also being a number) and should not be replaced with 99?





Originally Posted by masijade View Post
When you initiate the array (i.e. int[][] array = new int[20][20];) every position is already filled with 0 (since a primitive cannot be null). So, if zero is not one of the numbers you are using, simply cycle through the arrays with a nested loop replacing 0 with 99.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 102
Reputation: TheGathering is an unknown quantity at this point 
Solved Threads: 10
TheGathering's Avatar
TheGathering TheGathering is offline Offline
Junior Poster

Re: two dimensional arrays

 
0
  #5
Aug 8th, 2007
Originally Posted by anamika_nagpur View Post
Agree that a premitive cannot be null. But what if one of the position is actually filled with number zero [0] from a text file (zero also being a number) and should not be replaced with 99?
Then don't replace it with 99? Think, you're going from 2 states: nothing or number, to three states: nothing, number, or protected.

Easy way to do that is to make a nested loop to set all values to some other value than 0 such as -1 as soon as you make your 2D array. That way you will know that all -1 spots are fair game, all number > 0 spots are already set, and all 0 spots are untouchable.


That what you were talking about?
Last edited by TheGathering; Aug 8th, 2007 at 11:50 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,417
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 257
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: two dimensional arrays

 
0
  #6
Aug 8th, 2007
Are you using Java 1.5 or later? If so, make it an Integer array, rather than an int array. Then "empty" spots will be null and you just (after having filled it with the random numbers) cycle through with an == null check. And, you can still do array[i][j] = 1; or some other int, because autoboxing will automatically convert it to an Integer. You can also, later, use the Integer as if it were a normal int. That seems to be easiest solution.
Last edited by masijade; Aug 8th, 2007 at 2:48 pm. Reason: changed integer to int for clarity
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: Turendur is an unknown quantity at this point 
Solved Threads: 1
Turendur's Avatar
Turendur Turendur is offline Offline
Newbie Poster

Re: two dimensional arrays

 
0
  #7
Aug 8th, 2007
Why doesn't he just fill the made arrays with 99 from the start then if you have a number of 0 it replaces a 99 thus you wont have to go back and check if the numbers are 0's. Although you would have to loop through first so you are adding more process time.

Just thought I would comment.

Tell me if I'm wrong...

Turendur
Last edited by Turendur; Aug 8th, 2007 at 4:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 2
Reputation: anamika_nagpur is an unknown quantity at this point 
Solved Threads: 1
anamika_nagpur anamika_nagpur is offline Offline
Newbie Poster

Re: two dimensional arrays

 
0
  #8
Aug 14th, 2007
This appears to be the close fit solution. Attempting to fill the slots with any number (be it zero, 99 or any negative number) on array initialization is a processing burden and doesn't address the original problem of filling the array with the same number as that used for initialization. Having an Object array will allow null value and subsequent comparison during traversing the array and fill it with ANY number (zero, positive or negative intiger) as the need be.
Thanks for this solution.

Originally Posted by masijade View Post
Are you using Java 1.5 or later? If so, make it an Integer array, rather than an int array. Then "empty" spots will be null and you just (after having filled it with the random numbers) cycle through with an == null check. And, you can still do array[i][j] = 1; or some other int, because autoboxing will automatically convert it to an Integer. You can also, later, use the Integer as if it were a normal int. That seems to be easiest solution.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC