Everytime I run this method my array is reset - java

Everytime I call set() it resets all the values in the array to false except for what ever the int row int col is because i set that to true before the method ends. Why is this happening I thought I was making a copy of the array B and then setting the values that are in A to the values in B? Or am I mistaken here.
public void set(int row, int col) throws IndexOutOfBoundsException {
if (row >capacityr) {
boolean B[][] = new boolean[row+1][capacityc+1];
for (int k = 0; k < capacityr; k++)
for (int j = 0; j < capacityc; j++)
B[k][j] = a[k][j];
capacityr=row;
a = B;
}
if (col >capacityc) {
boolean C[][] = new boolean[capacityr+1][col+1];
for (int k = 0; k <capacityr; k++)
for (int j = 0; j < capacityc; j++)
C[k][j] = a[k][j];
capacityc=col;
a = C;
}
a[row][col] = true;
pT++;
}

It should be easier to use an ArrayList but I think this would fix your problem.
public void set(int row, int col) throws IndexOutOfBoundsException {
if(row > capacityr) {
if(col > capacityc) {
//both row and col are too big
boolean temp[][] = new boolean[row+1][col+1];
//copy a
for(int i = 0; i <= capacityr; i++) {
for(int j = 0; j <= capacityc; j++) {
temp[i][j] = a[i][j];
}
}
//set all the new elements to false
for(int i = capacityr+1; i <= row; i++) {
for(int j = capacityc+1; j <= col; j++) {
temp[i][j] = false;
}
}
//set row and col and a to temp
temp[row][col] = true;
a = temp;
//update capacity
capacityr = row;
capacityc = col;
}
else {
//just row is too big
boolean temp[][] = new boolean[row+1][capacityc+1];
for(int i = 0; i <= capacityr; i++) {
for(int j = 0; j <= capacityc; j++) {
temp[i][j] = a[i][j];
}
}
for(int i = capacityr+1; i <= row; i++) {
temp[i][capacityc] = false;
}
temp[row][col] = true;
a = temp;
capacityr = row;
}
}
else {
if(col > capacityc) {
//just col is too big
boolean temp[][] = new boolean[capacityr+1][col+1];
for(int i = 0; i <= capacityr; i++) {
for(int j = 0; j <= capacityc; j++) {
temp[i][j] = a[i][j];
}
}
for(int j = capacityc+1; j <= col; j++) {
temp[capacityr][j] = false;
}
temp[row][col] = true;
a = temp;
capacityc = col;
}
else {
//neither are too big
a[row][col] = true;
}
}
}

Related

Sudoku check subboxes for duplicates

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;
}

Java - Sorting a 2D Array by Row Sum

Trying to write a method that swaps the rows of a 2D array in order of increasing row sum.
For example, if I have the following 2d array:
int [][] array = {4,5,6},{3,4,5},{2,3,4};
I would want it to output an array as so:
{2 3 4}, {3 4 5}, {4 5 6}
Methodology:
a.) take the sums of each row and make a 1D array of the sums
b.) do a bubble sort on rowSum array
c.) swap the rows of the original array based on the bubble sort swaps made
d.) then print the newly row sorted array.
Here's my code so far:
public void sortedArrayByRowTot() {
int [][] tempArray2 = new int [salaryArray.length [salaryArray[0].length];
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[i].length; j++) {
tempArray2[i][j] = salaryArray[i][j];
}
}
int [] rowSums = new int [tempArray2.length];
int sum = 0;
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[i].length; j++) {
sum += tempArray2[i][j];
}
rowSums[i] = sum;
sum = 0;
}
int temp;
int i = -1;
for(int j = rowSums.length; j > 0; j--){
boolean isSwap = false;
for (i = 1; i < j; i++) {
if(rowSums[i-1] > rowSums[i]) {
temp = rowSums[i-1];
rowSums[i-1] = rowSums[i];
rowSums[i] = temp;
isSwap = true;
}
}
if(!isSwap){
break;
}
}
for (int k = 0; k < tempArray2.length; k++) {
temp = tempArray2[i-1][k];
tempArray2[i-1][k] = tempArray2[i][k];
tempArray2[i][k] = temp;
}
for (int b = 0; b < tempArray2.length; b++) {
for (int c = 0; c < tempArray2[b].length; c++) {
System.out.print(tempArray2[b][c] + " ");
}
}
}
}
Not sure if I am doing part c of my methodology correctly?
It keeps saying "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2"
As #shmosel said, you can do it like this:
public static void sortedArrayByRowTot() {
int [][] array = {{4,5,6},{3,4,5},{2,3,4}};
Arrays.sort(array, Comparator.comparingInt(a -> IntStream.of(a).sum()));
}
I was able to solve my question. Thanks.
public void sortedArrayByRowTot() {
//Creates tempArray2 to copy salaryArray into
int [][] tempArray2 = new int [salaryArray.length][salaryArray[0].length];
//Copies salaryArray into tempArray2
for (int i = 0; i < salaryArray.length; i++) {
for (int j = 0; j < salaryArray[i].length; j++) {
tempArray2[i][j] = salaryArray[i][j];
}
}
//Creates rowSum array to store sum of each row
int [] rowSums = new int [tempArray2.length];
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[0].length; j++) {
rowSums[i] += tempArray2[i][j];
}
}
//Modified Bubble Sort of rowSum array (highest to lowest values)
int temp;
int i = 0;
for(int j = rowSums.length; j > 0; j--){
boolean isSwap = false;
for (i = 1; i < j; i++) {
if(rowSums[i-1] < rowSums[i]) {
temp = rowSums[i-1];
rowSums[i-1] = rowSums[i];
rowSums[i] = temp;
isSwap = true;
//swaps rows in corresponding tempArray2
int [] temp2 = tempArray2[i-1];
tempArray2[i-1] = tempArray2[i];
tempArray2[i] = temp2;
}
}
if(!isSwap){
break;
}
}
//Prints sorted array
System.out.println("Sorted array: ");
for (i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[i].length; j++) {
System.out.print("$"+ tempArray2[i][j] + " ");
}
System.out.println();
}
}
You may try this way. That I have solved.
public class Solution{
public static void sortedArrayByRowTot() {
int [][] salaryArray = { {4,5,6},{3,4,5},{2,3,4} };
int [][] tempArray2 = new int [salaryArray.length][salaryArray[0].length];
for (int i = 0; i < salaryArray.length; i++) {
for (int j = 0; j < salaryArray[i].length; j++) {
tempArray2[i][j] = salaryArray[i][j];
}
}
// Buble Sort to store rowSums
int [] rowSums = new int [tempArray2.length];
for (int i = 0; i < tempArray2.length; i++) {
for (int j = 0; j < tempArray2[0].length; j++) {
rowSums[i] += tempArray2[i][j];
}
}
//Buble Sort by Rows Sum (Lowest Value to Highest)
int temp;
int i = 0;
for(int j = rowSums.length; j > 0; j--){
boolean isSwap = false;
for (i = 1; i < j; i++) {
if(rowSums[i-1] > rowSums[i]) {
temp = rowSums[i-1];
rowSums[i-1] = rowSums[i];
rowSums[i] = temp;
isSwap = true;
//swaps rows in corresponding tempArray2
int [] temp2 = tempArray2[i-1];
tempArray2[i-1] = tempArray2[i];
tempArray2[i] = temp2;
}
}
if(!isSwap){
break;
}
}
/** No Need.
for (int k = 0; k < tempArray2.length; k++) {
temp = tempArray2[i-1][k];
tempArray2[i-1][k] = tempArray2[i][k];
tempArray2[i][k] = temp;
}
*/
for (int b = 0; b < tempArray2.length; b++) {
for (int c = 0; c < tempArray2[b].length; c++) {
System.out.print(tempArray2[b][c] + " ");
}
}
}
public static void main(String[] args) {
sortedArrayByRowTot();
}
}

Two-Dimensional Loop OutOfBoundsException - Java

I was wondering if you guys could help me out with some of my code. I have been programming this puzzle creator, but when I attempt to generate values for my two-dimensional array, I get an OutOfBoundsException. I'm sure this solution is simple, but for whatever reason, I can't seem to find what's wrong. (Also, of course, there is a driver class that goes along with this, but I don't think that's necessary to include here.)
import java.util.*;
public class ThirtyWonderful
{
private int dimen;
public ThirtyWonderful (int dimensions)
{
dimen = dimensions;
}
public ThirtyWonderful()
{
dimen = 5;
}
Random gen = new Random();
int[][] nums = new int [dimen][dimen];
public void genPuzzle()
{
for (int count = 0; count < dimen; count++)
{
for(int col = 0; col < dimen; col++)
{
nums[count][col] = gen.nextInt(9) + 1;
}
}
checkAcc();
if(checkAcc() == true)
{
for (int count = 0; count < dimen; count++)
{
for(int col = 0; col < dimen; col++)
{
System.out.print(nums[count][col] + " ");
}
System.out.println();
}
}
}
public boolean checkAcc()
{
int tot = 0;
for (int count = 0; count < dimen; count++)
{
for(int col = 0; col < dimen; col++)
{
tot += nums[count][col];
}
if(tot != 31)
return false;
}
for (int count = 0; count < dimen; count++)
{
for(int col = 0; col < dimen; col++)
{
tot += nums[count][col];
}
if(tot != 31)
return false;
}
return true;
}
}
You are declaring nums with new int [dimen][dimen]; Before dimen even initialization, By default dimen value is 0, so nums is initialize with new int[0][0]
Here is the Fix Code
public class ThirtyWonderful {
private int dimen;
Random gen = new Random();
int[][] nums;
public ThirtyWonderful(int dimensions) {
this.dimen = dimensions;
nums = new int[dimen][dimen];
}
public ThirtyWonderful() {
this(5);
}
public void genPuzzle() {
for (int count = 0; count < dimen; count++) {
for (int col = 0; col < dimen; col++) {
nums[count][col] = gen.nextInt(9) + 1;
}
}
checkAcc();
if (checkAcc() == true) {
for (int count = 0; count < dimen; count++) {
for (int col = 0; col < dimen; col++) {
System.out.print(nums[count][col] + " ");
}
System.out.println();
}
}
}
public boolean checkAcc() {
int tot = 0;
for (int count = 0; count < dimen; count++) {
for (int col = 0; col < dimen; col++) {
tot += nums[count][col];
}
if (tot != 31) {
return false;
}
}
for (int count = 0; count < dimen; count++) {
for (int col = 0; col < dimen; col++) {
tot += nums[count][col];
}
if (tot != 31) {
return false;
}
}
return true;
}
}
you should initialize your array object in your constructors. you are initializing your array at class load time before your object is instantiated.

Filling a ragged array with a loop in java

I was wondering how it was possible to fill a ragged array with a loop in Java.
In my example I want to put Pascals Triangle in the Array. When I try to do it ragged, (
// int [][] hako = new int [n][]; -> as I understand it; it gives a java.lang.NullPointerException.
Thanks
int n = 12, r = 1;
int [][] hako = new int [n][];
for(int i = 0; i < n; i++){
for(int j = 0; j < r; j++){
hako[i][j] = newton(i, j);
}
r++;
}
static int factorial(int n){
int k = 1;
if(n == 0)
return 1;
while(n>1){
k*=n;
n--;
}
return k;
}
static int newton(int i, int j){
return factorial(i)/((factorial(j))*(factorial(i-j)));
}
You need to initialize hako[i] as an array before you can assign a variable to an index within it i.e. hako[i][j].
int n = 12, r = 1;
int [][] hako = new int [n][];
for(int i = 0; i < n; i++){
for(int j = 0; j < r; j++){
// need to initialize hako[i]
hako[i] = new int[r];
hako[i][j] = newton(i, j);
}
r++;
}
your hako, is a matrix, but you initialize only one dimension thus your NullPointerException
to fix it try
for(int i = 0; i < n; i++){
hako[i] = new int[r];
for(int j = 0; j < r; j++){
hako[i][j] = newton(i, j);
}
r++;
}
public static void main(String[] args) {
int y[][] = new int[4][];
int four =4;
for (int row = 0; row < y.length; row++) {
y[row] = new int[four--];
}
RaggedArray(y);
for (int row = 0; row < y.length; row++) {
for (int column = 0; column < y[row].length; column++) {
System.out.print(y[row][column] + " ");
}
System.out.println();
}
}
public static void RaggedArray(int x[][]) {
int j;
for (int i = 0; i < x.length; i++) {
int k=1;
for (j = 0;j<x[i].length ; j++) {
x[i][j] = k++;
}
}
}}
You can change the size and fill it with any data. I wish it will be useful for u and anyone see this code.

Building a tester class?

I am a very new java user, and I have always had trouble with testing my programs. Here I have implement a basic boolean array interface. I want to test it buy adding a couple values and then running each of the interface methods. Please be nice I am not very good at this but I want to get better.
package booleanmatrix;
import java.util.Arrays;
/**
*
* #author David
*/
public class ArrayMatrix implements BooleanMatrixs {
private boolean a[][];
public int Rows;
public int Cols;
public int capacityr = 1;
public int capacityc = 1;
public int pT=0;
public int pF=0;
public ArrayMatrix() {
a = new boolean[capacityr][capacityc];
}
#Override
public int getNumberRows() {
return capacityr;
}
#Override
public int getNumberCols() {
return capacityc;
}
#Override
public void set(int row, int col) throws IndexOutOfBoundsException {
if (row > capacityr) {
capacityr = row;
boolean B[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
B[k][j] = a[k][j];
}
}
a = B;
}
if (col > capacityc) {
capacityc = col;
boolean C[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
C[k][j] = a[k][j];
}
}
a = C;
}
a[row][col] = true;
pT++;
}
#Override
public void clear(int row, int col) throws IndexOutOfBoundsException {
if (row > capacityr) {
capacityr = row;
boolean B[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
B[k][j] = a[k][j];
}
}
a = B;
}
if (col > capacityc) {
capacityc = col;
boolean C[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
C[k][j] = a[k][j];
}
}
a = C;
}
a[row][col] = false;
}
#Override
public void set(int row, int col, boolean value) throws IndexOutOfBoundsException {
if (row > capacityr) {
capacityr = row;
boolean B[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
B[k][j] = a[k][j];
}
}
a = B;
}
if (col > capacityc) {
capacityc = col;
boolean C[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
C[k][j] = a[k][j];
}
}
a = C;
}
a[row][col] = value;
if(value==true){
pT++;
}
}
#Override
public void toggle(int row, int col) throws IndexOutOfBoundsException {
if (row > capacityr) {
capacityr = row;
boolean B[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
B[k][j] = a[k][j];
}
}
a = B;
}
if (col > capacityc) {
capacityc = col;
boolean C[][] = new boolean[capacityr][capacityc];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
C[k][j] = a[k][j];
}
}
a = C;
}
if (a[row][col] == true) {
a[row][col] = false;
pT--;
} else {
a[row][col] = true;
pT++;
}
}
#Override
public void setAll() {
Arrays.fill(a, Boolean.TRUE);
pT=(capacityr*capacityc);
}
#Override
public void clearAll() {
Arrays.fill(a, Boolean.FALSE);
pT=0;
}
#Override
public void setAll(boolean value) {
if (value == false) {
Arrays.fill(a, Boolean.FALSE);
} else {
Arrays.fill(a, Boolean.TRUE);
pT=(capacityr*capacityc);
}
}
#Override
public boolean get(int row, int col) throws IndexOutOfBoundsException {
boolean x;
x = a[row][col];
return x;
}
#Override
public int[][] getTruePositions() {
int count = 0;
int ret[][] = new int[pT][2];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
if (a[k][j] == true) {
ret[count][0] = k;
ret[count][1] = j;
count++;
}
}
}
return ret;
}
#Override
public int[][] getFalsePositions() {
int total = (capacityr*capacityc);
int P=(total-pT);
int count=0;
int ret[][] = new int[P][2];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
if (a[k][j] == false) {
ret[count][0] = k;
ret[count][1] = j;
count++;
}
}
}
return ret;
}
#Override
public int[][] getPositions(boolean value) {
int total = (capacityr*capacityc);
int P=(total-pT);
int count=0;
if(value==false){
int retf[][] = new int[P][2];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
if (a[k][j] == false) {
retf[count][0] = k;
retf[count][1] = j;
count++;
}
}
}
return retf;}
else{
int count2 = 0;
int ret[][] = new int[pT][2];
for (int k = 0; k < capacityr; k++) {
for (int j = 0; j < capacityc; j++) {
if (a[k][j] == true) {
ret[count2][0] = k;
ret[count2][1] = j;
count2++;
}
}
}
return ret;
}}
#Override
public int getNumberTrueValues() {
return pT;
}
#Override
public int getNumberFalseValues() {
int total = (capacityr*capacityc);
int P=(total-pT);
return P;
}
#Override
public int getNumberValues(boolean value) {
if (value==false){
int total = (capacityr*capacityc);
int P=(total-pT);
return P;
}
else{return pT;}}
#Override
public String toString(){
String X= "1)The number of rows"+capacityr+"\n 2)The number of Columns"+capacityc+"\n 3)The number of True Values"+pT+"\n 4)The number of false values"+(capacityc*capacityr-pT);
return X;
}
public static void main(String[] args) {
}
}
Take a look at this JUnit tutorial. It might be just a bit of overkill, but a good unit testing framework is usually the best way to test your code at this level.
Add a new class that has a Main method (if you are using eclipse there is an auto click button that does that for you) then instantiate a new variable from your class.
ArrayMatrix arrayVar = new ArrayMatrix();
Then you would be able to access all the methods such as:
arrayVar.getNumberCols();

Categories