JOptionPane showOptionDialog shows up blank randomly - java

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".

Related

why is my board not being rendered in the same orientation as the 2D array

With this code I'm getting the board in the picture. can any please help me figure out why this is happening and how I fix it.
I've tried calling super.paintComponent but nothing is changing. if i just needed the board I would have probably left it but I implemented a search and the search kept going to the wrong position because the array doesnt match the board which I dont understand because the array is defining the board. This problem is beyond my abilities and help is greatly appreciated.
protected int[][] board = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 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, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 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, 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, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0 },
{ 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0 },
{ 0, 1, 0, 1, 1,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 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
#Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[0].length ; col++) {
Color color;
switch (board[row][col]) {
case 0:
color = new Color(149, 153, 146);
break;
default:
color = new Color(38, 252, 0);
}
g2.setColor(color);
g2.fillRect(row * pixelSize, col * pixelSize, pixelSize, pixelSize);
}
}
}
Change this:
g2.fillRect(row * pixelSize, col * pixelSize, pixelSize, pixelSize);
to this:
g2.fillRect(col * pixelSize, row * pixelSize, pixelSize, pixelSize);
X coordinate comes before Y.
X is defined by column.
Y is defined by row.

Filling an array with all combinations of formula N^R

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

Java random distribution

I have an array of Objects which is [36] long. I have to randomly distribute these objects between owners, one object can only have one owner. The user can set 2 - 5 owners and it goes like this:
2 owners - 14 Object / owner - 8 Object without owner
3 owners - 10 / owner - 6 empty
4 owners - 8 / owner - 4 empty
5 owners - 6 / owner - 6 empty
I want to ask for example in case 2 how can i set 6 random Objects owner to 0 (without owner), and 10 random Objects for each owner?
It's not exactly clear from the question what you are trying to accomplish, but you can achieve some shuffling with the Collections.shuffle() function.
int[] shuffle = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
List<Integer> intList = new ArrayList<Integer>();
for (int i : shuffle) {
intList.add(i);
}
Collections.shuffle(intList);
System.out.println(intList);
One possible result:
[1, 2, 0, 2, 2, 1, 1, 1, 2, 2, 2, 1, 0, 1, 1, 0, 0, 2, 1, 2, 2, 1, 0, 1, 0, 0, 2, 1, 2, 2, 1, 2, 1, 2, 0, 1]
References:
shuffle
Problem Solved
First i created a 2d array which contains the 4 different case
public int[][] shuffle = {{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{ 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4},
{ 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5}};
Then in a method i copied out the needed row to a new array based on user input. I used Fisher - Yates shuffle on the primitive 1d array and looped threw the Objects where i called a setter methor for each owner.
Looks like this:
for(int i = 0; i < objectArray.length; i++){
int helper = shuffle1d[i];
objectArray[i].setObjectOwner(helper);
}

Java: Generating tile based circles of any size

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, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1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, 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]

Android java assigning 2d array to 3d array

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;
*/

Categories