I want to reverse a boolean array. free is the array. Here's my code:
public boolean[][] free = new boolean[6][6];
free = !free;
I get an error saying the operator ! is undefined. What should I do?
EDIT
It is not empty. I just want to inverse the values.
public boolean[][] free = new boolean[6][6];
void initFree(State s) {
for (int i = 0; i < nbcars; i++){
if (horiz[i]){
for (int j = 0; j < (len[i]-1); j++)
free[moveon[i]][s.pos[i]+j]=true;
}
if (!horiz[i]){
for (int j = 0; j < (len[i]-1); j++)
free[s.pos[i]+j][moveon[i]]=true;
}
}
free = !free;
}
You get operator ! is undefined simply because Java doesn't define ! operator on a two dimensional boolean array.
One way you could achieve what you want is by iterating over each value and then use the ! operator.
for(int i=0; i<free.length; i++)
{
for(int j=0; j<free[i].length; j++)
{
free[i][j] != free[i][j];
}
}
Here's a simple example you can modify for your uses. I included an invert method.
public class StackOverflowExample {
public static void main(String[] args) {
int ROW = 10, COL = 10;
boolean[][] ATwoDBoolArray = new boolean[ROW][COL];
PutValuesInArray(ATwoDBoolArray);
printArray(ATwoDBoolArray);
invertArray(ATwoDBoolArray);
printArray(ATwoDBoolArray);
}
private static boolean[][] PutValuesInArray(boolean array[][]) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = false;
}
}
return array;
}
private static void printArray(boolean[][] array){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println(array[i][j]);
}
}
}
private static boolean[][] invertArray(boolean array[][]){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if(array[i][j]){
array[i][j] = false;
}else{
array[i][j] = true;
}
}
}
return array;
}
}
This method inverts all the values in a given 2-dimensional boolean array.
static void invertBooleanArray(boolean[][] arr) {
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
arr[i][j] = !arr[i][j];
}
Related
i have to check the validity of a sudoku as a homework. the rows and cols were no big deal, but i'm stuck at checking the boxes.
i want to loop through the 3x3 sub-boxes and check them for duplicates, with copying the numbers into a smaller 3x3 array, flattening it and then checking it for duplicates.
my while loop doesn't seem to make sense tho, the first iteration works but the second one writes wrong values into my new array. i just don't know where to add my boxcount.
i feel like a noob, if someone can help i'd be thankful
private static boolean isValidSudokuSolution(int[][] sudokuField) {
// TODO: Implementieren Sie hier Ihre Lösung für die Methode
//rows
int row = 0;
while (row < sudokuField.length) {
for (int i = 0; i < sudokuField[row].length; i++) {
for (int j = 0; j < i; j++) {
if (sudokuField[row][i] == sudokuField[row][j]) {
return false;
}
}
}
row++;
}
//cols
int col = 0;
while (col < sudokuField.length) {
for (int i = 0; i < sudokuField[col].length; i++) {
for (int j = 0; j < i; j++) {
if (sudokuField[i][col] == sudokuField[j][col]) {
return false;
}
}
}
col++;
}
//box
int boxCount = 0;
while (boxCount < sudokuField.length) {
//create box array
int[][] box = new int[3][3];
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box[i].length; j++) {
box[i][j] = sudokuField[i + boxCount][j];
}
}
//flatten box
int[] flattenedBox = new int[sSize];
int counter = 0;
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box[i].length; j++) {
int num = box[i][j];
flattenedBox[counter + j] = num;
}
counter += 3;
}
//look for duplicates
for (int i = 0; i < flattenedBox.length; i++) {
for (int j = 0; j < i; j++) {
if (flattenedBox[i] == flattenedBox[j]) {
return false;
}
}
}
boxCount += 3;
}
return true;
}
I have a two dimensional array and I fill it with scanner. I want to copy the elements that start with letter 'a' to a new one dimensional array without using ArrayList. Please advise on what I can do to get this code functioning properly. the question is how can I know the new array size while I don't know how many words start with letter a
Here is what I have so far:
import java.util.Scanner;
class Untitled {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[][] name = new String[2][2];
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < name[i].length; j++) {
name[i][j] = input.next();
}
}
student(name);
}
public static void student(String[][] arr) {
int count = 0;
int c2 = -1;
String[] name2 = new String[count];
String temp = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
c2++;
temp = arr[i][j];
name2[c2] = temp;
count++;
temp = "";
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
}
}
A two dimensional arrray of size [n][n] is equal to one dimensional array of size n. If you want to copy them on proper place then you can use this formula, it is useful if you later want to copy these elements back to twodimensional array at proper places:
int v = i * n + j; // i and j your loops and n is length of rows or colums.
array[v] = array[i][j];
for in your codes it's like:
int v = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
v = i * arra.length +j;
name2[v] = arr[i][j];
count++;
Ok here is a working code:
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String[][] name = new String[2][2];
System.out.println("Enter the name: ");
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < name[i].length; j++) {
name[i][j] = input.next();
}
}
student(name);
}
public static void student(String[][] arr) {
int count = 0;
int v = 0;
String[] name2 = new String[arr.length*arr[0].length];
String temp = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
v = i *+arr[0].length + j;
name2[v] = arr[i][j];
count++;
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
System.out.println("printing without nulls");
//if you don't want null to be printed then do this:
for (int i = 0; i < name2.length; i++) {
if(name2[i] != null)
System.out.println(name2[i]);
}
}
I did it with two nested for loop one for indicating the array size and the other for filling the elements into the array, it does the work but is there any way to do this better
public static void student(String[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
size++;
}
}//inner
}//outer
String[] name2 = new String[size];
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j].charAt(0) == 'a') {
name2[count] = arr[i][j];
count++;
}
}//inner
}//outer
for (int i = 0; i < name2.length; i++) {
System.out.println(name2[i]);
}
I have a method transpose in class Matice. When i apply this method on my m2.matice i can see during debugging that my matrix is being transposed, but when i print m2 i get the same matrix as before.
public class Main {
public static void main(String[] args) {
Matice m1 = new Matice(5);
m1.matrixFill(0, 5);
m1.matrixPrint();
Matice m2 = new Matice(3);
m2.matrixFill(-5, 10);
m2.matrixPrint();
m2.transpose();
m2.matrixPrint();
}
}
package matice;
/**
*
* #author Adam Rychter
*/
public class Matice {
int[][] matice;
private int n;
public Matice(int n) {
this.n = n;
if(n > 0) {
matice = new int[n][n];
}
}
public void matrixPrint(){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.format("%5d", matice[i][j]);
}
System.out.println("");
}
System.out.println("");
}
public void matrixFill(int a, int b){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matice[i][j] = (int) (Math.random() * (a + b + 1) - a);
}
}
}
public void transpose(){
int tmp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
tmp = matice[i][j];
matice[i][j] = matice[j][i];
matice[j][i] = tmp;
}
}
}
}
Your problem is that you're transposing each element twice ! once when i=row, j=col, and once when i=col, j=row, with the net effect of leaving the matrix as it was. The easiest way I see to fix it is do the swap only if i>j (and then test to make sure :) this is a suggestion, not working code :)
Note that j goes from i to n, not 1 to n; also note need for two tmp variables.
public void transpose(){
int tmp1, tmp2;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
tmp1 = matice[i][j];
tmp2 = matice[j][i];
matice[i][j] = tmp2;
matice[j][i] = tmp1;
}
}
Like this, maybe:
public void transpose(){
Matice mL = new Matice(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
mL.matice[i][j] = matice[j][i];
}
}
matice = mL.matice;
}
See alternate, more efficient answer above.
public void transpose(){
int tmp;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
tmp = matice[i][j];
matice[i][j] = matice[j][i];
matice[j][i] = tmp;
}
}
}
I want to know why the for-loop counts through the args three times and doesn't stop after the first loop? this is my code :
public class test2 {
public static void main(String[] args) {
int N = args.length;
int[] x = new int[N];
int count = 0;
for (int i = 0; i < x.length; i++) {
x[i] = Integer.parseInt(args[i]);
}
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (x[j] != x[j-1]) {
count++;
}
System.out.println(count);
}
}
System.out.println(N-count);
}
}
I think you mean to do this:
for (int j = 1; j < N; j++) {
if (x[j] != x[j-1]) {
count++;
}
System.out.println(count);
}
System.out.println(N-count);
instead of:
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (x[j] != x[j-1]) {
count++;
}
System.out.println(count);
}
}
System.out.println(N-count);
Do anybody have a function with which I can transpose a Matrix in Java which has the following form:
double[][]
I have function like this:
public static double[][] transposeMatrix(double [][] m){
for (int i = 0; i < m.length; i++) {
for (int j = i+1; j < m[0].length; j++) {
double temp = m[i][j];
m[i][j] = m[j][i];
m[j][i] = temp;
}
}
return m;
}
but its wrong somewhere.
public static double[][] transposeMatrix(double [][] m){
double[][] temp = new double[m[0].length][m.length];
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
temp[j][i] = m[i][j];
return temp;
}
If you would like to use an external library, Apache Commons Math provides the utility to transpose a matrix. Please refer to it official site.
First, you have to create a double array double[][] arr, as you have already done. Then, the transposed 2d matrix can be achieved like this
MatrixUtils.createRealMatrix(arr).transpose().getData()
Since Java 8, you can do this:
public static double[][] transposeMatrix(final double[][] matrix) {
return IntStream.range(0, matrix[0].length)
.mapToObj(i -> Stream.of(matrix).mapToDouble(row -> row[i]).toArray())
.toArray(double[][]::new);
}
Java Class to Transpose a matrix :-
import java.util.Scanner;
public class Transpose {
/**
* #param args
*/
static int col;
static int row;
static int[][] trans_arr = new int[col][row];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
col = m;
int n = sc.nextInt();
row = n;
int[][] arr = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arr[i][j] = sc.nextInt();
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
int[][] trans_arr = new int[col][row];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
trans_arr[j][i] = arr[i][j];
}
}
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
System.out.print(trans_arr[i][j] + " ");
}
System.out.println();
}
}
}
Here is the method
public static double[][] transpose(double arr[][]){
int m = arr.length;
int n = arr[0].length;
double ret[][] = new double[n][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
ret[j][i] = arr[i][j];
}
}
return ret;
}
Here is a code to transpose a two dimensional matrix "In Place" (not using another data structure to save output) and hence is more memory efficient:
Same below algorithm can be used for int or char or string data types as well.
public static double[][] transposeDoubleMatrix(double[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
double tmp = matrix[j][i];
matrix[j][i] = matrix[i][j];
matrix[i][j] = tmp;
}
}
return matrix;
}
Here's a small change!
for (int j = i; j < m[0].length; j++)