These objects all have an attribute thats called number, i want to now add to every object in this 2d array a number, starting from 0 to 99.
So tiles[0][0] has the number 0 and tiles[9][9] has number 99. tiles[3][0] should have number 2.
I tried putting this in a nested for loop like this:
for(int x = 0; x < mapWidth; x++)
{
for(int y = 0; y < mapHeight; y++)
{
for(int i = 0; i <100; i++){
tiles[x][y].nummer(i);
}
}
}
but with this the nummer is always 99? it isnt 0 to 99
How can i do this in a for loop or something?
SO MY QUESTION IS:
how can i make it so that the nummer for the tiles goes from 0 to 99?
int counter = 0;
for(int y = 0; y < mapHeight; y++)
{
for(int x = 0; x < mapWidth; x++)
{
tiles[x][y].number(counter);
counter++;
}
}
it's because you are running a loop that sets the number from 0 to 99 on every tile, so you end up with 99 everywhere.
Instead you should do something like:
for(int i = 0 ; i < 100 ; i++) {
tiles[i%10][i/10] = i;
}
You could also just use a variable to keep the counter
int num = 0;
for (int y = 0; y < mapHeight; y++) {
for (int x = 0; x < mapWidth; x++) {
tiles[x][y].nummer(num);
num++;
}
}
Related
I'm currently creating a brickbreaker (or breakout, like atari breakout) and for the bricks im using a 2d array:
private Brick bricks[][] = new Brick[10][41];
To destroy a specific Brick (for whatever reason i have to destroy that), i created a method:
public void destroyBrick(Brick brick) {
for(int x = 0; x < bricks[x].length; x++) {
for(int y = 0; y < bricks[y].length; y++) {
if (bricks[x][y].equals(brick)) {
bricks[x][y] = null;
}
}
}
}
The way the array is created is the following:
for(int j=0; j<bricks[0].length; j++){
for(int i=0; i<bricks.length; i++){
bricks[i][j]=new Brick(game,10,Color.GREEN,j*margin,i*margin);
}
}
Now for some reason the counter for x eventually reaches 10 and the counter for y reaches 0 where it crashes, and i can't figure out why.
Is there a different way of iterating a 2d array that has different values for columns and rows?
note
The reason the formatting and my spelling is so rubbish is, that im writing this on the app version. Feel free to correct any mistakes or formatting errors, before i can.
You are referencing y in the loop that iterates on it. This compiles correctly, because y is defined at the point of reference, but it does not do what you need. In addition, the outer loop should stop upon reaching bricks.length, not bricks[x].length.
These two lines
for(int x = 0; x < bricks[x].length; x++) {
for(int y = 0; y < bricks[y].length; y++) {
should be
for(int x = 0; x < bricks.length; x++) {
// ^
for(int y = 0; y < bricks[x].length; y++) {
// ^
The first int x doesn't refer to what you need, I think.
x ranges from 0 to the length of the array indexed with x, that is 41.
for (int x = 0; x < bricks.length; x++)
for (int y = 0; y < bricks[x].length; y++)
Should work (at least I wrote this without testing)
Try iterating with any of this
public void destroyBrick(Brick brick) {
for(int x = 0; x < bricks.length; x++) {
for(int y = 0; y < bricks[0].length; y++) {
if (bricks[x][y].equals(brick)) {
bricks[x][y] = null;
}
}
}
}
or
public void destroyBrick(Brick brick) {
for(int x = 0; x < bricks.length; x++) {
for(int y = 0; y < bricks[x].length; y++) {
if (bricks[x][y].equals(brick)) {
bricks[x][y] = null;
}
}
}
}
if there's any other error, use your debugger to find out where exactly
I'm a little stuck on the arithmetic for this program. I have a string theString that has a length of x*y*z. x, y, and z are the dimensions of a char[][][] array (lets call it cArr).
I want to insert the characters of theString into cArr based on a specific index. Here's an example of a 5x4x3 array.
String theString = someMethod(); //returns a string of length 60
char[][][] cArr = new char[5][4][3];
for (int z = 0; z < 3; z++) {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y][z] = theString.charAt(/*~~~*/);
}
}
}
I can figure out the arithmetic to insert characters into a char[5][4] array from a string of length 20:
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y] = theString.charAt((5*y)+x);
}
}
What arithmetic should I be using in the charAt() method for a 3-dimensional char array?
My best attempt has been:
char[][][] cArr = new char[5][4][3];
for (int z = 0; z < 3; z++) {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y][z] = theString.charAt(((3^2)*z)+(4*y)+x);
}
}
}
But this makes all z-index layers of cArr the same.
The rule is simple for problems like this. You start from the innermost loop and proceed up with x + y*(length of x) + z*(length of y)*(length of x).
In this example, it would be,
cArr[x][y][z] = theString.charAt(x + y*5 + z*4*5);
If I have
String[][] trenches = new String[10][10];
How can i make all elements in row 0 have the value of "X" ?
or all elements in row 1 have the value of "0" ?
You can do this using a for loop.
int row = 0;
int value = 5;
for(int i = 0; i < trenches[row].length(); i ++)
{
tenches[row][i] = value;
}
You could put this in a function and pass the row and value
public void standardiseRow(int row, int value)
Think of your 2d array as an x and y coordinate system. You can have a double for loop that goes through your x multiple times, but goes through your y only one time. In this case, your outer loop would be y and your inner loop would be x.
for (int y = 0; y < array.length; y++) {
for (int x = 0; x < array.length; x++) {
if (y == 0) {
array[x][y] = X;
}
if (y == 1) {
array[x][y] = 0;
}
}
}
Try this code:
String[][] trenches = new String[10][10];
trenches[0] = StringUtils.repeat("X", 10).split("");
trenches[1] = StringUtils.repeat("0", 10).split("");
So I have been working on this problem for a while now. I keep getting an ArrayIndexOutOfBoundsException but I am unable to locate where the issue lies. If someone could point me in the right direction, I would really appreciate it! Thanks!
public class Answer {
public static void main(String[] args){
double[] y = {23, 11.1, 50.4};
double[] x = {22.2, 46, 100.0};
Answer answer = new Answer();
answer.answer(y, x);
}
public static int answer(double[] y, double[] x) {
int result = 0;
double percent_1, percent_2;
double[] compareList_1 = new double[x.length];
double[] compareList_2 = new double[y.length];
// Calculate percent of first 2 x value array items with y
// all y values. Store the results in a seperate list.
for(int i = 0; i < x.length; i++){
percent_1 = compare(y[i], x[0]);
percent_2 = compare(y[i], x[1]);
compareList_1[i] = percent_1;
compareList_2[i] = percent_2;
}
// Compare those lists to find common number
// There you have your answer.
result = (int)compareLists(compareList_1, compareList_2);
return result;
}
// Calculates percentage from x and y values
public static double compare(double y, double x){
double result = 1 - (y/x);
return result;
}
// Finds common value in lists
public static double compareLists(double[] list_1, double[] list_2){
for(int i = 0; i < list_1.length + 1; i++){
for(int j = 0; j < list_2.length + 1; j++){
if(list_1[i] == list_2[j]){
return list_1[i];
}
}
}
// Just cus this shouldn't ever return.
return 100;
}
}
In your iteration (compareLists), you should use 'length' (not length + 1)
for(int i = 0; i < list_1.length; i++)
for(int j = 0; j < list_2.length; i++)
I think the problerm is in
for(int i = 0; i < list_1.length + 1; i++){
for(int j = 0; j < list_2.length + 1; j++){
i < list_1.length + 1 or j < list_2.length + 1 change it to
for(int i = 0; i < list_1.length; i++){
for(int j = 0; j < list_2.length ; j++){
remove +1 from each condition.For j < list_2.length + 1 the list_2.length will give you length of array ie lastIndex +1 and you are adding another +1 in it causing loop condition to be j<lastIndex +1 giving you index error on the last iteration of loop in the line if(list_1[i] == list_2[j]){ for list_2[j]
Also in answer method you declare array by
double[] compareList_1 = new double[x.length];
double[] compareList_2 = new double[y.length];
and in the loop you are iterating upto x.length if x.length is greater than y.length the you can get the Index error in compareList_2[i] = percent_2;(inside the loop) because its length is y.length.
I am trying to take an array that contains 400 integers, and split it into a 20x20 2-D array. I thought I had the right algorithm, but the sum of the 1-D array does not match the sum of the 2-D array, so I’m obviously doing something wrong. Here is my code:
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
for (int z = 0; z < 400; z++)
{
array[x][y] = inArray[z];
}
}
}
}
What am I doing wrong?
For each pair of x and y, your code assigns every value from inArray to array[x][y]. This is clearly incorrect.
Here is one way to fix the code:
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0, z = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[z++];
}
}
}
You're assigning the last element in the input array to every element in the output array. The inner loop over the input array must be changed into a single assignment that picks the correct input element.
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[x * 20 + y]; // or devise your own calculation
}
}
}
The current approach will assign each element in the array with the last element in the original.
You should not iterate z but use it as a counter, in the same loops.
int z = 0;
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[z++];
}
}
You're basically assigning array[x][y] equal to inArray[0], then inArray[1], then inArray[2], all the way to [400].
I would do something like
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[x * 20 + y];
}
}
}