This question is for Java. I keep on trying to create a Block[][] of size N, where the numbers 0 - 9 are placed randomly. I've created an array and then shuffled it to prevent numbers from being repeated. When I iterate over the array and assign the numbers to the two dimensional array, I keep on finding that all of the numbers in the two dimensional array are the same. The block keeps on coming out as:
222
222
222
I'd greatly appreciate your help!
public class Board {
int N = 3;
static int [][] copy;
//construct a board from an N-by-N array of blocks
//(where blocks[i][j] = block in row i, column j)
public Board(int[][] blocks){
blocks = new int[N][N]; //creates array of size N
//generates random numbers 0 inclusive to # exclusive
List<Integer> randomList = new ArrayList<>();
for (int i = 0; i < blocks.length; i++){
randomList.add(i);
}
int randomNum = 0;
for (int i = 0; i < blocks.length; i++){
randomNum = randomList.get(i);
}
Collections.shuffle(randomList);
for (int i = 0; i < blocks.length; i++){
for (int j = 0; j < blocks[i].length; j++){
blocks[i][j] = randomNum;
}
}
copy = blocks.clone();
}
int counter = 0;
Collections.shuffle(randomList);
for (int i = 0; i < blocks.length; i++){
for (int j = 0; j < blocks[i].length; j++){
blocks[i][j] = randomList.get(counter);
counter++;
}
}
You should create a counter that will loop through each element of your original arraylist and assign to the multi-dimensional arraylist
Also change:
for (int i = 0; i < 10; i++){
randomList.add(i);
}
You say you want 0-9 the above loop will give you 0-9. The loop you have will only give you 0-2.
The problem is you're assigning the value from randomNum to all of your cells. This variable is defined before your second for-loop and is left at the highest value at which that loop terminated (in your example the value 2).
You probably want to add a value from your randomList and then reshuffle it when the i index changes.
Your code for selecting a random number has no randomness in it at all:
int randomNum = 0;
for (int i = 0; i < blocks.length; i++){
randomNum = randomList.get(i);
}
You do shuffle randomList afterwards, but then make no use of the shuffled list.
You need to use the value in your random list of the position you are setting. In addition to this, if you want 0-9 values to be inserted, you need to modify the random list creation:
public Board(int[][] blocks){
blocks = new int[N][N]; //creates array of size N
//generates random numbers 0 inclusive to # exclusive
List<Integer> randomList = new ArrayList<>();
for (int i = 0; i < 10; i++){
randomList.add(i);
}
Collections.shuffle(randomList);
for (int i = 0; i < blocks.length; i++){
for (int j = 0; j < blocks[i].length; j++){
blocks[i][j] = randomList.get(j);
}
// Shuffle the list again to get different values for the next line
Collections.shuffle(randomList);
}
copy = blocks.clone();
}
Related
I would like to remove a particular number from the array
Integer[] arr = new Integer[7];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
Collections.shuffle(Arrays.asList(arr));
This is creating numbers from 0-7
But I dont need 0,I need value from 1-7
The first value written into your array is 0 because you initialize i with 0 in the for loop.
Therefore your loop will only insert the values 0 - 6.
Change this initialisation to i = 1 and in addition you also need to change the condition of the for loop to arr.length + 1 or i <= arr.length, so it will count up to 7.
Integer[] arr = new Integer[7];
for (int i = 1; i < arr.length + 1; i++) {
arr[i] = i;
}
What you also can do instead of changing the loop itself, is to change the loop body. Just add 1 to i when assigning it to arr[i]:
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
In this case i will count from 0 to 6, and assign 1 to 7 to your array
Change your int i = 0 to int i = 1 like this:
Integer[] arr = new Integer[7];
for(int i = 1; i <8; i++){
int value = i-1;
arr[value] = i;
}
Collections.shuffle(Arrays.asList(arr));
for(int i = 0; i < arr.length; i++){
System.out.println("Result:"+arr[i]);
}
Console message:
Result:7
Result:2
Result:6
Result:5
Result:4
Result:1
Result:3
I want to create an 8x8 array with JAVA, in which i want to have 1 eight times in random generated positions. All the other positions of the array are going to be 0. I am using this code but obviously it is not filling the array with 1 for a specific number of times.
public static void main(String[] args) {
int [][] arr = new int [8][8];
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
arr[i][j] = (int) (Math.random()*2);
}
}
for(int k = 0; k < 8; k++){
for(int l = 0; l < 8; l++){
System.out.print(arr[k][l] + " ");
}
System.out.println();
}
}
}
import java.util.Random;
int[][] array = new int[8][8];
Random r = new Random();
int a = r.nextInt(8);
int b = r.nextInt(8);
//insert 8 random 1's in the 8x8 matrix, no duplicates
//by default in Java the other places are
for(int i = 0; i < 8; i++){
while(array[a][b] == 1){
a = r.nextInt(8);
b = r.nextInt(8);
}
array[a][b] = 1;
}
I'm having trouble filling a matrix with values I get by iterating through a method. I want to use a 3x3 matrix and then fill it with the values I obtain by iterating my method from 0 to 8. My idea was a for-loop but it does not work unfortunately. I would be glad if someone could help or has a link where I can look that up.
int[][] matrix = new int[3][3];
for (int i = 0; i < matrix.length; i++){
for (int j = 0; j < matrix[i].length; j++){
for(int a = 0; a < 9; a++) {
matrix[i][j] = fields.get(a).getSign().getFieldValue();
}
}
}
Correct me if I'm wrong.
The way I understood your question was you want to fill the matrix like this:
012
345
678
In that case you can do the first 2 forloops and add some maths, to get the correct numbers on every position:
int[][] matrix = new int[3][3];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = i * matrix[i].length + j;
}
}
The way this works is for every row (i) you multiply the rownumber by the rows length (the number of columns) and add the current column to it
To iterate through a two dimensional field (e.g. a matrix), you can use two for loops:
int dimension = 3;
int [][] matrix = new int [dimension][dimension];
for (int i = 0; i < dimension; i++){
for ( int j = 0; j < dimension; j++){
matrix[i][j] = fields.get(i).get(j);
}
}
I don't exactly know how you want to retrieve the values, but your current call looks suspicious to say the least :-)
It will simply assign the same value to all ints in column j.
If you simulate your loop, this is what it looks like
i -> 0
j -> 0
matrix[0][0] -> loop over all values from field
this way you would be putting the fields.get(8) into each index in your matrix.
#Alan's answers show how to properly loop and fill a 2d matrix from a 1d matrix
I am looking for the dimensions of a 2d array. Not the fixed values assigned to the array, but the values containing integers. Example:
int a[][] = new int[4][5];
a[1][1] = 1;
a[1][2] = 2;
a[1][3] = -2;
a[1][4] = 0;
a[2][1] = -3;
a[2][2] = 4;
a[2][3] = 7;
a[2][4] = 2;
a[3][1] = 6;
a[3][2] = 0;
a[3][3] = 3;
a[3][4] = 1;
Only 3 variables are used out of 4, and 4 variables used out of 5. How can you tell what is actually there versus what is assigned to be there?
array.length;
array[0].length;
The previous code will only give you the fixed variable assigned to the array. But what if your not using all the variables? In other words I am looking for an output of 3 for columns and 4 for rows.
In your code example, you are using all variables in the array. Just because you haven't assigned anything to them, they are still sitting there.
If you want to determine whether or not you've assigned a value to each element in the array, you need to initialize everything to a default value, and then just compare against the default value.
A variation on that option is to declare them as int?, which is a nullable int. That would allow you to set everything to null, and then you'll know that the elements that are no longer null have had a value assigned to them.
You can't do it dynamically. Once you've initiated your matrix like that (new int[4][5]) you have and matrix of 0.
You must initiate the matrix ant set an invalid value to all posistions, something like -1 or whatever you wan't and iterate over it to discover the result
// using Integer instead of int, which is a nullable object
Integer a[][] = new Integer[4][5];
int cnt1 = 0;
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
if(a[i][j] != null) {
cnt1++;
}
}
}
System.out.println(cnt1 + " array indices are non-null.");
// using int
int b[][] = new int[4][5];
for(int i = 0; i < b.length; i++) {
for(int j = 0; j < b[i].length; j++) {
b[i][j] = -9999;
}
}
int cnt2 = 0;
for(int i = 0; i < b.length; i++) {
for(int j = 0; j < b[i].length; j++) {
// pick a value that will never be assigned
if(b[i][j] != -9999) {
cnt2++;
}
}
}
System.out.println(cnt2 + " array indices are not -9999.");
Can someone please explain the thought process behind this code? I am kind of confused on how it works. This is the question that the code is addressing:
Write code (using one or more loops) to fill an array "a" with 10 different random numbers between 1 and 10.
Thank you so much for any help!
public static void main(String[] args){
//R8.8
int a[] = new int[10];
Random randomGenerator = new Random();
for (int i = 0; i < a.length; i++){
a[i] = 1 + randomGenerator.nextInt(100);
}
for (int i = 0; i < a.length; i++) {
int number = 1 + randomGenerator.nextInt(100);
int count = 0;
for (int j = 0; j < i; j++) {
if (a[j] == number) {
count += 1;
}
}
if (count > 0) i -= 1;
else a[i] = number;
}
}
}
See my comments in the code itself:
public static void main(String[] args){
//make a new array of 10 integers
int a[] = new int[10];
//declare an object which we can use to generate random numbers
//this object probably uses the system time to generate numbers that appear random
//but at the end of the day, java does it for us so
//we don't really need to know or care how it generates random numbers
Random randomGenerator = new Random();
//loop over each element in our array
for (int i = 0; i < a.length; i++){
//for each element, set that element to a random between 1 and 100 inclusive
//nextInt(x) gets a number between 0 (inclusive) and x (not inclusive)
//so to translate that to 1 to x inclusive, we need to add 1 to the result
a[i] = 1 + randomGenerator.nextInt(100);
}
//everything below here does literally nothing to solve the problem
//everything you need to fill the array with random numbers is above
for (int i = 0; i < a.length; i++) {
int number = 1 + randomGenerator.nextInt(100);
int count = 0;
for (int j = 0; j < i; j++) {
if (a[j] == number) {
count += 1;
}
}
if (count > 0) i -= 1;
else a[i] = number;
}
}
}
Please note that you should use 1 + randomGenerator.nextInt(10); to fill the array with numbers between 1 and 10, not 1 + randomGenerator.nextInt(100);.