944,036 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 8193
  • Java RSS
Jul 26th, 2007
0

two dimensional arrays

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
baltazar is offline Offline
46 posts
since Jul 2007
Jul 26th, 2007
0

Re: two dimensional arrays

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 )
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jul 26th, 2007
0

Re: two dimensional arrays

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".
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 8th, 2007
0

Re: two dimensional arrays

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?





Click to Expand / Collapse  Quote originally posted by masijade ...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
anamika_nagpur is offline Offline
2 posts
since Apr 2007
Aug 8th, 2007
0

Re: two dimensional arrays

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.
Reputation Points: 22
Solved Threads: 10
Junior Poster
TheGathering is offline Offline
102 posts
since Jul 2007
Aug 8th, 2007
0

Re: two dimensional arrays

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
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Aug 8th, 2007
0

Re: two dimensional arrays

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Turendur is offline Offline
10 posts
since Aug 2007
Aug 14th, 2007
0

Re: two dimensional arrays

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.

Click to Expand / Collapse  Quote originally posted by masijade ...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
anamika_nagpur is offline Offline
2 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: I want learn about core JAVA....
Next Thread in Java Forum Timeline: save file to database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC