Related
This question already has answers here:
Plus one leetcode
(7 answers)
Closed 4 months ago.
I am trying to solve Plus One on Leetcode
The description is as follows:
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].
My code is:
class Solution {
public int[] plusOne(int[] digits) {
long temp=0;int c=0;
for(int i:digits)
{
temp=temp*10+i;
//System.out.println(temp);
}
//System.out.println(temp);
temp+=1;
//System.out.println(temp);
long copy=temp;
while(copy>0)
{
copy/=10;
c++;
}
int[] result=new int[c];
for(int i=c-1;i>=0;i--)
{
//System.out.println((int)temp%10);
result[i]=(int)temp%10;
// System.out.println(result[i]);
temp/=10;
}
return result;
}
}
The problem is when I am trying to extract the number in the unit's digit it is givig 9 instead of 1.
Expected output:[9,8,7,6,5,4,3,2,1,1]
My Output:[9,8,7,6,5,4,3,2,1,9]
There is nothing really wrong with your code...it just needs a little tweak.
The problem you are having is within the for loop that converts the incremented number held in temp to an int[] Array. Casting the temp % 10 to an int as in result[i] = (int) temp % 10; is going to produce an undesirable result. temp is a long and therefore shouldn't be cast to int until is has finished its modulo calculation. So, the simple solution would be to wrap the equation within parentheses, for example:
result[i] = (int) (temp % 10);
That should solve the immediate problem. An even bigger problem is the fact that you are limited to a numerical value that can only fit into a long data type. An int[] Array can hold an extremely large number if you were to take each element of that array and sequentially combine the digits. A long data type for example can only hold an unsigned value of 9223372036854775807. As you can see, this would create an int[] Array of 19 elements and that's a small Array.
What if the Array contained 100 elements (digits)? That's a pretty big number but not necessarily out of the realm of use. This is why you have been advised (in comments) to utilize the java.math.BigInteger class. With BigInteger, you can work with any size of integer number which is basically what you really need. Here is how you could carry out the same task using BigInteger:
public int[] plusOne(int[] digits) {
java.math.BigInteger num = java.math.BigInteger.ZERO;
// Convert int[] Array to a BigInteger number...
for (int b : digits) {
num = num.multiply(java.math.BigInteger.valueOf(10))
.add(java.math.BigInteger.valueOf(b));
}
// Add 1 to the determined BigInteger number
num = num.add(java.math.BigInteger.valueOf(1));
// Convert BigInteger number back to an Array
digits = num.toString().chars().map(c -> c - '0').toArray();
return digits;
}
As an example, let's run this method against an int[] Array of 100 elements. That would be an integer number consisting of 100 digits. Now that's a large number:
/* Auto-create an int[] array containing 100 elements of random digits
0 to 9 (the first digit of the Array will never be a 0). */
int[] array = new int[100];
for (int i = 0; i < 100; i++) {
int n = new java.util.Random().nextInt(9);
// Make sure the first digit isn't a 0.
if (i == 0 && n == 0) {
i--;
continue;
}
array[i] = new java.util.Random().nextInt(9);
}
System.out.println("Original: -> " + Arrays.toString(array));
/* Cycle the plusOne() method 12 times and display the array
within each cycle... */
for (int i = 0; i < 12; i++) {
array = plusOne(array);
// Display The array:
System.out.println("Plus One: -> " + Arrays.toString(array));
}
The Console Window should display something like this. You will need to scroll to the end to see the incrementing (plus one):
Original: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 2, 6]
Plus One: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0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 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 3, 7]
Plus One: -> [7, 8, 8, 3, 7, 3, 2, 1, 4, 6, 8, 8, 0, 7, 5, 8, 1, 7, 7, 0, 2, 2, 3, 2, 7, 2, 2, 2, 3, 4, 6, 7, 4, 6, 1, 4, 2, 4, 3, 1, 0, 4, 2, 8, 3, 4, 3, 8, 3, 6, 2, 5, 6, 6, 3, 5, 1, 3, 0, 7, 0, 0, 8, 7, 0, 0, 2, 4, 8, 5, 0, 6, 0, 2, 3, 5, 0, 1, 3, 3, 4, 1, 2, 8, 8, 1, 8, 3, 0, 5, 8, 8, 2, 2, 6, 6, 3, 5, 3, 8]
This is pretty straight forward solution. Just what you do in the school: increment right to left with keeping carry for the next digit.
class Solution {
public int[] plusOne(int[] digits) {
boolean carry = true;
for (int i = digits.length - 1; carry && i >= 0; i--) {
carry = digits[i] == 9;
digits[i] = carry ? 0 : digits[i] + 1;
}
if (carry) {
int[] tmp = new int[digits.length + 1];
tmp[0] = 1;
System.arraycopy(digits, 0, tmp, 1, digits.length);
digits = tmp;
}
return digits;
}
}
For a homework question i need to fill an array with all combinations of the formula N^R. The variable R is constant and is 6. The variable N is not constant and let's say it's 2. So 2^6 = 64. Now what i need is an array with all the combinations (in this case 64). I found a website which does exactly what i need and the output in this case should be:
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1],
[0, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 0, 1],
[0, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 1, 1],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 1],
[0, 0, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1],
[0, 1, 0, 0, 1, 0],
[0, 1, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 0],
[0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 0],
[0, 1, 0, 1, 1, 1],
[0, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 1],
[0, 1, 1, 0, 1, 0],
[0, 1, 1, 0, 1, 1],
[0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 1],
[0, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 0],
[1, 0, 0, 0, 1, 1],
[1, 0, 0, 1, 0, 0],
[1, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 1, 0],
[1, 0, 0, 1, 1, 1],
[1, 0, 1, 0, 0, 0],
[1, 0, 1, 0, 0, 1],
[1, 0, 1, 0, 1, 0],
[1, 0, 1, 0, 1, 1],
[1, 0, 1, 1, 0, 0],
[1, 0, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0],
[1, 0, 1, 1, 1, 1],
[1, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0],
[1, 1, 0, 0, 1, 1],
[1, 1, 0, 1, 0, 0],
[1, 1, 0, 1, 0, 1],
[1, 1, 0, 1, 1, 0],
[1, 1, 0, 1, 1, 1],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 1],
[1, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 1, 1],
[1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 0, 1],
[1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1]
I have tried realising this with for loops, but without succes.
I don't want the full code of an algorithm that makes this possible, but
I want to be helped on my way. Thanks in advance.
I've come up with this solution, which is a bit clumsy but should probably work for your case, the comments should explain everything:
public static void printCombinations(int R, int N) {
// calculate the combinations
String[][] combinations = calculateCombinations(R, N);
// iterate over all
for (int i = 0; i < combinations.length; i++) {
// prints the commas at the end
if (i != 0) {
System.out.println(',');
}
// print to std out
System.out.print(Arrays.toString(combinations[i]));
}
System.out.println();
}
public static String[][] calculateCombinations(int R, int N) {
// calculate our limit
int limit = (int) StrictMath.pow(N, R);
// create the result array
String[][] result = new String[limit][R];
// iterate over all possibilities
for (int i = 0; i < limit; i++) {
// convert to base
String base = Long.toString(i, N);
// holds our temporary value
StringBuilder intermediate = new StringBuilder(R);
// pad the value from the start with zeroes if needed
for (int sub = R - base.length(); sub > 0; sub--) {
intermediate.append('0');
}
// append our number
intermediate.append(base);
// append to result
result[i] = intermediate.toString().split("");
}
// return the result
return result;
}
And can then be called like this to pretty print it out:
printCombinations(6, 2);
Or to get it as a result:
String[][] result = calculateCombinations(6, 2);
Running Demo
I took a few Java programming classes in college but it's been a few years so I'm pretty rusty. I decided to ask my brother for a programming task. He asked me to write a program to quiz him on the elemental type effectiveness in the Pokemon games. The code works for the most part, but for some reason the Option Dialog box is blank sometimes. I have a title, but the rest is a gray box. The part that's most confusing to me is that I have a println() right before it and it prints the correct phrase when the pane is empty.
Here is my code output:
import javax.swing.JOptionPane;
public class Quiz {
public static void main(String[] args) {
try{
/*creates the matrix of the different types
*and their effectiveness to each other. 0
*represents "Not very effective", 1 is "Neutral"
*2 is "Super effective", 3 is "No Damage"
*/
int[][] myTypeArray =
{{1, 1, 1, 1, 1, 0, 1, 3, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{2, 1, 0, 0, 1, 2, 0, 3, 2, 1, 1, 1, 1, 0, 2, 1, 2, 0},
{1, 2, 1, 1, 1, 0, 2, 1, 0, 1, 1, 2, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 0, 1, 0, 3, 1, 1, 2, 1, 1, 1, 1, 1, 2},
{1, 1, 3, 2, 1, 2, 0, 1, 2, 2, 1, 0, 2, 1, 1, 1, 1, 1},
{1, 0, 2, 1, 0, 1, 2, 1, 0, 2, 1, 1, 1, 1, 2, 1, 1, 1},
{1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 2, 1, 2, 1, 1, 2, 0},
{3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 0, 1},
{1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 1, 0, 1, 2, 1, 1, 2},
{1, 1, 1, 1, 1, 0, 2, 1, 2, 0, 0, 2, 1, 1, 2, 0, 1, 1},
{1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 0, 0, 1, 1 ,1, 0, 1, 1},
{1, 1, 0, 0, 2, 2, 0, 1, 0, 0, 2, 0, 1, 1, 1, 0, 1, 1},
{1, 1, 2, 1, 3, 1, 1, 1, 1, 1, 2, 0, 0, 1, 1, 0, 1, 1},
{1, 2, 1, 2, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 3, 1},
{1, 1, 2, 1, 2, 1, 1, 1, 0, 0, 0, 2, 1, 1, 0, 2, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 2, 1, 3},
{1, 0, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 0, 0},
{1, 2, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 2, 2, 1}};
//Names for the types
String[] myTypeNamesArray = {"Normal", "Fighting", "Flying", "Poison", "Ground",
"Rock", "Bug", "Ghost", "Steel", "Fire", "Water",
"Grass", "Electric", "Psychic", "Ice", "Dragon",
"Dark", "Fairy"};
//loops the message panes until they get a wrong answer
for(int i = 0; i > -1; i++){
//Two integers to randomly select one of the 18 different types
int num1 = (int)(Math.random()*18);
int num2 = (int)(Math.random()*18);
//Creates JOptionPane to show the two randomed types and get input
//Name of the buttons
Object[] buttons = { "Not Very Effective", "Neutral", "Super Effective", "No Damage" };
//Test output for asking them how effective type 1 is vs type 2
System.out.println(num1 + " " + num2 + " " + "How effective is " +
myTypeNamesArray[num1] + " against " + myTypeNamesArray[num2] + "? " +
"the answer was " + (String)buttons[myTypeArray[num1][num2]] + ". " + i);
// **HERE IS WHERE THE MESSAGE IS BLANK SOMETIMES**
int answer = JOptionPane.showOptionDialog(null, "How effective is " +
myTypeNamesArray[num1] + " against " + myTypeNamesArray[num2] + "?",
"Pokemon Type Effectiveness Quiz", JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[0]);
//Test their answers
if(answer == myTypeArray[num1][num2]){
} else if(!(answer == myTypeArray[num1][num2])) {
// **THIS IS ALSO BLANK SOMETIMES **
JOptionPane.showMessageDialog(null, "Sorry, the answer was " +
(String)buttons[myTypeArray[num1][num2]] + ". You said " +
buttons[answer] + "\nYou got " + i + " correct.");
break;
} else {
break;
}
}
} catch (Exception e) {
System.out.println("The error was " + e);
}
}//end main
}
The intermittent nature of the problem suggests a threading issue. Try running your code on the Swing event thread. Something like:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
doOnEventThread();
}
});
}
public static void doOnEventThread() {
try{
/*creates the matrix of the different types
*and their effectiveness to each other. 0
*represents "Not very effective", 1 is "Neutral"
*2 is "Super effective", 3 is "No Damage"
*/
int[][] myTypeArray =
{{1, 1, 1, 1, 1, 0, 1, 3, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{2, 1, 0, 0, 1, 2, 0, 3, 2, 1, 1, 1, 1, 0, 2, 1, 2, 0},
{1, 2, 1, 1, 1, 0, 2, 1, 0, 1, 1, 2, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 0, 1, 0, 3, 1, 1, 2, 1, 1, 1, 1, 1, 2},
{1, 1, 3, 2, 1, 2, 0, 1, 2, 2, 1, 0, 2, 1, 1, 1, 1, 1},
{1, 0, 2, 1, 0, 1, 2, 1, 0, 2, 1, 1, 1, 1, 2, 1, 1, 1},
{1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 2, 1, 2, 1, 1, 2, 0},
{3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 0, 1},
{1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 1, 0, 1, 2, 1, 1, 2},
{1, 1, 1, 1, 1, 0, 2, 1, 2, 0, 0, 2, 1, 1, 2, 0, 1, 1},
{1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 0, 0, 1, 1 ,1, 0, 1, 1},
{1, 1, 0, 0, 2, 2, 0, 1, 0, 0, 2, 0, 1, 1, 1, 0, 1, 1},
{1, 1, 2, 1, 3, 1, 1, 1, 1, 1, 2, 0, 0, 1, 1, 0, 1, 1},
{1, 2, 1, 2, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 3, 1},
{1, 1, 2, 1, 2, 1, 1, 1, 0, 0, 0, 2, 1, 1, 0, 2, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 2, 1, 3},
{1, 0, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 0, 0},
{1, 2, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 2, 2, 1}};
//Names for the types
String[] myTypeNamesArray = {"Normal", "Fighting", "Flying", "Poison", "Ground",
"Rock", "Bug", "Ghost", "Steel", "Fire", "Water",
"Grass", "Electric", "Psychic", "Ice", "Dragon",
"Dark", "Fairy"};
//loops the message panes until they get a wrong answer
for(int i = 0; i > -1; i++){
//Two integers to randomly select one of the 18 different types
int num1 = (int)(Math.random()*18);
int num2 = (int)(Math.random()*18);
//Creates JOptionPane to show the two randomed types and get input
//Name of the buttons
Object[] buttons = { "Not Very Effective", "Neutral", "Super Effective", "No Damage" };
//Test output for asking them how effective type 1 is vs type 2
System.out.println(num1 + " " + num2 + " " + "How effective is " +
myTypeNamesArray[num1] + " against " + myTypeNamesArray[num2] + "? " +
"the answer was " + (String)buttons[myTypeArray[num1][num2]] + ". " + i);
// **HERE IS WHERE THE MESSAGE IS BLANK SOMETIMES**
int answer = JOptionPane.showOptionDialog(null, "How effective is " +
myTypeNamesArray[num1] + " against " + myTypeNamesArray[num2] + "?",
"Pokemon Type Effectiveness Quiz", JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[0]);
//Test their answers
if(answer == myTypeArray[num1][num2]){
} else if(!(answer == myTypeArray[num1][num2])) {
// **THIS IS ALSO BLANK SOMETIMES **
JOptionPane.showMessageDialog(null, "Sorry, the answer was " +
(String)buttons[myTypeArray[num1][num2]] + ". You said " +
buttons[answer] + "\nYou got " + i + " correct.");
break;
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Edit
For more information on Swing's event thread and threading issues, please have a look at the article, Concurrency in Swing. But to re-iterate, whenever you find yourself up against a program misbehavior that's intermittent, that doesn't happen all the time, think "threading problem".
Above is a pattern I have been trying to figure out how to generate for quite some time now. I pretty much want to input the radius of the circle into a method and have it return an array with the proper values in it. (Ex. 0 = darkest, 1 = less dark, 2 = less less dark, etc)
My problem is is that I have no idea how this could be done mathematically. Every time the radius increases, the edges of the bounding square are eroded even more. I do not see a clear pattern in change between sizes 3, 4, 5, and so on. Can anyone help me? Thanks!
Here is some info I came up with:
Radius of 3 means (x-2,y-2) (x-2, y-1) and (x-1, y-2) are not changed on the left side and every other respective corner.
Radius of 4 means (x-3, y-1) (x-3, y-2) (x-3,y-3) (x-2, y-2) (x-2, y-3) and (x-1, y-3) are not changed on the left side and every other respective corner.
x center point - (Radius - 1) = left, x center point + (Radius - 1) = right
y center point - (Radius - 1) = top, y center point - (Radius - 1) = bottom
This question is extremely confusing, not least which due to the fact that your example doesn't look at all like a circle. #pst is exactly right. You can draw a circle to an offscreen buffer and then use that for your output.
For instance,
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Arrays;
/**
* #author Nicholas Dunn
* #date 4/21/12
*/
public class CircleGridCreator {
/**
*
* #param radius
* #return 2 dimensional array in row major order, where entry is 0 if not part
* of circle, or 1 otherwise
*/
public static int[][] getGrid(int radius) {
if (radius < 0) {
throw new IllegalArgumentException("Invalid radius " + radius);
}
BufferedImage buffer = new BufferedImage(radius*2, radius*2, BufferedImage.TYPE_INT_RGB);
Color c = Color.BLUE;
Graphics2D context = buffer.createGraphics();
context.setColor(c);
context.fillOval(0, 0, radius * 2, radius * 2);
int[][] results = new int[radius*2][radius*2];
for (int row = 0; row < radius*2; row++) {
for (int col = 0; col < radius*2; col++) {
if (buffer.getRGB(col, row) == c.getRGB()) {
results[row][col] = 1;
}
}
}
return results;
}
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
int[][] grid = getGrid(i);
for (int[] row : grid) {
System.out.println(Arrays.toString(row));
}
}
}
}
Output:
[1, 1]
[1, 1]
[0, 1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
[0, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]
[0, 0, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1]
[0, 1, 1, 1, 1, 1, 1, 1]
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0]
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 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0, 0]
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]
I'm running into problems trying to assign a 2d array to a 3d array, so I thought i'd ask a question about 3d and 2d arrays.
Say I have a masterArray[][][] and wanted to put childArray1[][] and childArray2[][] into it.
This is how I have done it and was wondering if that is the correct way of applying it:
private int[][][] masterArray;
private int[][] childArray1 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 8, 1, 0, 1},
{1, 0, 7, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 9, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
private int[][] childArray2 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 7, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
{1, 1, 1, 9, 1, 1, 8, 0, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
Ok, so in my init method I use these some methods to set the child arrays into the master array. What I was curious about was how this exactly works. I assumed the following:
masterLevel = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
masterArray[currentLevel][x][y] = childArray1[x][y];
}
}
Would that work?
In my application things aren't working so I picking out code that I am not 100% sure on.
It doesn't really matter how you organize a 3d array as long as you put things in the same way as you take them out.
From your comment on another answer it seems that you are having problem with element order ([currentLevel][x][y] = childArray[y][x];)
It seems you mixed MAP_HEIGHT and MAP_WIDTH. It should be:
masterLevel = new int[MAX_LEVELS][MAP_HEIGHT][MAP_WIDTH];
then you can use:
master[currentLevel][x][y] = childArray[x][y];
In Java multi-d arrays are actually arrays of arrays. So they can even be disjoint.
In the code you posted you refer to a variable called currentLevel that you did not define. I am sure that is defined in some code you did not post. Also don't forget that arrays are zero index. This code should work.
masterArray = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int currentLevel = 0; currentLevel < MAX_LEVELS; currentLevel++) {
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
masterArray[currentLevel][x][y] = childArray1[x][y];
}
}
}
If you ever work with massive arrays and need speed then you could look at System.arrayCopy();
String[] arr1D;
String[][] arr2D;
String[][][] arr3D;
arr1D = new String[] { "1", "2", "3" };
//////////////////////////////////////////////////////////////////////////
//assign 1D array to element of 2D array
//////////////////////////////////////////////////////////////////////////
arr2D = new String[][] {
arr1D ,
arr1D ,
arr1D
};
/*
// OR
arr2D = new String[3][];
arr2D[0] = arr1D;
arr2D[1] = arr1D;
arr2D[2] = arr1D;
// OR
arr2D = new String[][] {
new String[] { "1", "2", "3" } ,
new String[] { "1", "2", "3" } ,
new String[] { "1", "2", "3" }
};
*/
//////////////////////////////////////////////////////////////////////////
//assign 2D array to element of 3D array
//////////////////////////////////////////////////////////////////////////
arr3D = new String[][][] {
arr2D ,
arr2D ,
arr2D
};
/*
// OR
arr3D = new String[3][][];
arr3D[0] = arr2D;
arr3D[1] = arr2D;
arr3D[2] = arr2D;
*/