| | |
two dimensional arrays
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 34
Reputation:
Solved Threads: 0
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?
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?
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
----------------------------------------------
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
•
•
Join Date: Apr 2007
Posts: 2
Reputation:
Solved Threads: 1
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?
•
•
•
•
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. •
•
•
•
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?
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.
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
----------------------------------------------
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
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
Just thought I would comment.
Tell me if I'm wrong...
Turendur
Last edited by Turendur; Aug 8th, 2007 at 4:58 pm.
•
•
Join Date: Apr 2007
Posts: 2
Reputation:
Solved Threads: 1
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.
Thanks for this solution.
•
•
•
•
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.
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- Addition with Two Dimensional Arrays (C++)
- Multi-dimensional Arrays: (Python)
- three dimensional arrays (PHP)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- How to implement multi-dimensional arrays in Python?? (Python)
- Arrays (C++)
Other Threads in the Java Forum
- Previous Thread: I want learn about core JAVA....
- Next Thread: save file to database
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






