Subtract Three dimension array from 2D - java

I have two data sets, the first one is 3D and the second is 2D.
How can I implement the subtract of the two sets.
:
import java.util.*;
class test28{
public static void main ( String[] args ) {
int [][][] arr1 = {{{6,3,9,0},{8,6,5,3}}};
int [][] arr2= {{6,3,9,0},{8,6,5,3}};
test28 test = new test28();
System.out.println(test.subtract(arr1,arr2));
}
public static int [][] subtract(int[][][] a, int[][] b) {
int [][] diff = new int[a.length][a[0].length];
for (int i = 0; i < a.length - 1; i++) {
for ( int j=0; j<a[0].length; j++){
for ( int k=0; k<a[0][0].length; k++) {
diff[0][i] = a[i][j][k]- b[i][j];
}
}
}
return diff;
}
}
the code below generates an error :
i changed the diff instantiated, then i have indexoutofbound error
at diff[0][i] = a[i][j][k]- b[i][j];

One reasonable explanation is the following:
int [][] diff = new int[a[0].length][a[0][0].length];
for ( int j=0; j<a[0].length; j++){
for ( int k=0; k<a[0][0].length; k++)
diff[j][k] = a[0][j][k]- b[j][k];
Of course the dimensions must match so you have to perform that checking.
I dont see the purpose of this.

Related

Merging two 2D arrays (M + N)

I am a first year programming trying to solve this challenge that was given to us students at uni.
Question image
There's a typo at where it says (N + K) whereas in fact it's actually (M+K) columns.
My attempt for this question goes as follows
public static int[][] mergeArrays(int[][] arrayA, int[][] arrayB){
int rows = 3;
int columns = arrayA[0].length + arrayB[0].length;
int[][] mergedArray = new int[rows][columns];
int k = 0;
for (int i = 0; i < rows; i++)
{
for ( int j = 0 ; j < columns; j++)
{
try
{
mergedArray[i][j] = arrayA[i][j];
}
catch (ArrayIndexOutOfBoundsException e)
{
mergedArray[i][j] = arrayB[i][k];
k += 1;
}
}
}
return mergedArray;
}
public static void main(String [] args)
{
int [][] a1 = { {1,2,3,3,3} , {3,2,1,6,3} , {4,5,6,1,3} };
int [][] a2 = { {1,9,7,2,3} , {0,7,8,3,2} , {3,8,9,7,2} };
int[][] m = mergeArrays(a1,a2);
for (int[] x : m)
{
for (int y : x)
{
System.out.print(y + " ");
}
System.out.println();
}
}
The program doesn't work for some reason. I don't know what's wrong with my approach here. Would really appreciate if someone helps me out.
Without using any libraries, in a manual way, here is my working answer.
I didn't use any of them, since we were not allowed, when I was a student.
public class Main {
private static int[][] mergeArrays(int[][] a1, int[][] a2) {
// Count rows and cols length.
int rows = a1.length;
int cols_a1 = a1[0].length;
int cols_a2 = a2[0].length;
// Total number of cols
int cols = cols_a2 + cols_a1;
int [][] merged = new int[rows][cols];
for (int i = 0; i < rows ; ++i) {
for (int j = 0; j < cols_a1; ++j) {
merged[i][j] = a1[i][j];
}
// To not overwrite values,
// the trick is to add an offset, while assigning,
// which is the amount of elements (cols_a1) used by the previous loop.
// Basically, we are shifting the k-index by this constant,
// as to not overwrite the values assigned from the previous
// inner loop.
for (int k = 0; k < cols_a2; ++k) {
merged[i][cols_a1 + k] = a2[i][k];
}
}
// Return the merged array
return merged;
}
// I refactored your good printing code into a method, for readability.
private static void print2darray(int[][] array2d) {
for (int[] x : array2d) {
for (int y : x) {
System.out.print(y + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int [][] a1 = {{1,2,3,3,3} , {3,2,1,6,3} , {4,5,6,1,3}};
int [][] a2 = {{1,9,7,2,3} , {0,7,8,3,2} , {3,8,9,7,2}};
int [][] merged = mergeArrays(a1, a2);
print2darray(merged);
}
}
The result is the same, as expected, from your question image:
1 2 3 3 3 1 9 7 2 3
3 2 1 6 3 0 7 8 3 2
4 5 6 1 3 3 8 9 7 2
Since you're a student I think to better if we give a hint, but since the solution is already there you can check this one as well:
public static int[] merge(int[] first,int[] second) {
return ArrayUtils.addAll(first, second);
}
public static void main(String[] args) {
int [][] a1 = { {1,2,3,3,3} , {3,2,1,6,3} , {4,5,6,1,3}};
int [][] a2 = { {1,9,7,2,3} , {0,7,8,3,2} , {3,8,9,7,2}};
int [][] a3 = new int[a1.length][];
for (int i = 0; i < a1.length; i++) {
a3[i] = merge(a1[i],a2[i]);
}
for (int[] ints : a3) {
StringJoiner joiner = new StringJoiner(",","[","]");
for (int i1 : ints) {
joiner.add(i1+"");
}
System.out.println(joiner.toString());
}
}
You are not merging it properly. Your logic is that if arrayA column index is out of bounds, you are adding from arrayB's columns. But what if that is also out of bounds, as in your case. Since you are always incrementing its index k. You could simply iterate over 2 arrays separately and merge into resulting array.
public static int[][] mergeArrays(int[][] arrayA, int[][] arrayB) {
int rows = 3;
int columns = arrayA[0].length + arrayB[0].length;
int[][] mergedArray = new int[rows][columns];
int k = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < arrayA[0].length; j++) {
mergedArray[i][k++] = arrayA[i][j];
}
for (int j = 0; j < arrayB[0].length; j++) {
mergedArray[i][k++] = arrayB[i][j];
}
k=0;
}
return mergedArray;
}

Returning a single array to a multidimensional array

Full disclosure; I needed to know this for an assignment. I wanted to return a single array to a multidimensional array from a method. I circumvented the issue with the below code by returning it to another 1-dimensional array then using a for loop to transfer values.
public class test
{
public static void main ( String args[] )
{
int[][] array1 = new int [100][5];
int[] temp = new int [5];
int num = 0;
temp = setValue();
for (int i = 0; i<=4; i++) // cycle 1
{
array1[num][i]= temp[i];
}
System.out.format("\n\n");
}
public static int[] setValue()
{
int[] array3 = new int [5];
for (int i = 0; i<=4; i++)
{
array3[i]= 2;
}
return array3;
}
}
Is there a more conventional way to return array3 to array1 without cycle 1? Something along the lines of
array1[num][] = setValue();
Comments:
The method returns a new array, so no need to initialize temp, or better yet, initialize it to return value:
int[] temp = setValue();
Java doesn't have 2D arrays, just arrays of arrays, so the entire inner array can be replaced, instead of copying values:
for (int i = 0; i <= 4; i++) // cycle 1
{
array1[num] = temp;
}
When you do that, you shouldn't allocate the inner arrays, i.e. replace [5] with []:
int[][] array1 = new int[100][];
Now there is actually no need for temp anymore, leaving main as just:
int[][] array1 = new int[100][];
int num = 0;
array1[num] = setValue();
Since you probably want to fill the entire 2D array:
int[][] array1 = new int[100][];
for (int num = 0; num < array1.length; num++) {
array1[num] = setValue();
}
As #VinceEmigh hinted above you can simply do array1[num] = setValue();
see
int arr[][] = new int[5][];
for (int x = 0; x < arr.length; x++) {
arr[x] = setValue();
}
for (int x = 0; x < arr.length; x++) {
for (int y = 0; y < arr[x].length; y++) {
System.out.println(arr[x][y]);
}
}

2D array - specify amount to new 2d array

I have a 2-d array of double numbers it's 48 by 48, I am trying to make a method that will allow the user to select a specific amount e.g. = 7 by 7 and then put that into a new 2d array.
public static double[][] amountOTP(double [][] a, int x){
a = new double[x][x];
return a;
}
thats all i have right now, this takes an 2d array as input however even though i specified x it doesn't work.
When you want to cut it ro a smaller size and copy the part of the original array, this should work:
public static double [][] cutArray (double [][] a, int newSize){
if (x > a.length)
throw new IllegalArgumentException ("Can only make array smaller");
double [][] b = new double [newSize][newSize];
for (int i = 0; i < newSize; i++){
for (int j = 0; j < newSize; j++){
b [i][j] = a [i][j];
}
}
return b;
}
The solution below considers situations in which the requested new two-dimensional array length is greater than the original in which case we simply return the original.
Example:
public static double[][] amountOTP(double [][] a, int x){
if(x > a.length) return a;
for (double[] arr : a)
if(arr.length < x)
return a;
double[][] newArray = new double[x][x];
for (int i = 0; i < x; i++)
for (int j = 0; j < x; j++)
newArray[i][j] = a[i][j];
return newArray;
}
Sorry if I'm wrong, but I'm assuming for now that you want your method to return a smaller 2d array which contains some of the values of the given array, in which case you would need to change your method to this:
public static double[][] amountOTP(double [][] a, int x) {
double[][] b = new double[x][x];
x = Math.min(x, a.length);
for(int i = 0;i < x; i++)
for(int j = 0;j < x; j++)
b[i][j] = a[i][j];
return b;
}
This should work fine but feel free to comment and inform me if I left anything out or it doesn't work; I'm here to help. Anyways, I hope this works for you. (Also, if this isn't the answer you were looking for, feel free to tell me.)
Note: This should avoid IndexOutOfBounds exceptions, so it will still work correctly if the user gives an x value bigger than the size of a. The 2d array that this method returns will just have some values of zero where it couldn't find any numbers.
I think it's something like that you're looking for :
public static double[][] amountOTP(double [][] a, int x){
double [][] ret = new double[x][x];
for(int i = 0; i < x; i++)
for(int j = 0; j < x; j++)
ret[i][j] = a[i][j];
return ret;
}
But you have to be careful with the parameters because it can cause an IndexOutOfBounds exception
I see you already have a lot of answers, but nobody was making use of the excellent Arrays.copyOf Java API method:
public static double[][] amountOTP(double [][] a, int x){
a = Arrays.copyOf(a, x);
for(int i=0; i<a.length; i++) {
if(a[i] != null) {
a[i] = Arrays.copyOf(a[i], x);
} else {
a[i] = new double[x]; // allows growth
}
}
return a;
}
you are actually not yet initializing the array so the method should be like this
public static void main(String[] args)
{
double[][] a=amountOTP(3);
for(int j=0;j<a.length;++j)
{
for(int i=0;i<a[j].length;++i)
{
a[j][i]=2;
}
}
for(int j=0;j<a.length;++j)
{
for(int i=0;i<a[j].length;++i)
{
System.out.println(a[j][i]);
}
}
}
public static double[][] amountOTP(int x)
{
return new double[x][x];
}

calculate the minimum value for each column in 2D array

I have a 2D array , iam trying to calculate the minimum value for each column and put the result in the result array.
the code bellow is calculating the minimum value for each row , how can i get the min value for each column.
import java.util.*;
class Test20 {
public static void main ( String [] args) {
int[][] array = {{6,3,9},
{0,8,2},
{3,7,5}};
Test20 test = new Test20();
System.out.print(Arrays.toString(test.mincol(array)));
}
public static int[] mincol (int[][] n) {
int[] result = new int[n.length];
for (int i = 0; i < n.length; i++) {
int min = n[0][i];
for (int j = 0; j < n[0].length; j++) {
if (n[j][i] < min) {
min = n[j][i];
}
}
result[i] = min;
}
return result;
}
}
Just change the loop the following way:
min = 0;
for(int i=0;i<n.length;i++){
for(int j=0;j<n[0].length;j++){
if(n[j][i]<n[j][min]){
min=j;
}
result[i]=n[min][i];
}
Be aware that you instantiate your result array by the length of the first dimension in your array but later use the n[][] param for looping and access the length of the second dimension in your loop.
If your two dim array is for example 4x5, this will cause ArrayOutOfBoundsExceptions.
You only need to do the same thing but inverting the variables
for(int i=0;i<n.length;i++){
for(int j=0;j<n[0].length;j++){
if(n[j][i]<n[min][j]){
min=i;
}
result[j]=n[min][j];
}
}
If your code is correct just change:
if(n[i][j]<n[i][min]){
min=j;
}
with
if(n[i][j]<n[result[i]][j]){
result[i]=i;
}
finally
for(int i=0;i<n.length;i++) result[i]=n[result[i][j];
you don't need min. But change
int [] result = new int[n.length];
to
int [] result = new int[n[0].length];
How about you transpose your two dimensional array like:
public static int[][] transpose (int[][] original) {
int[][] array = new int[original.length][];
// transpose
if (original.length > 0) {
for (int i = 0; i < original[0].length; i++) {
array[i] = new int[original[i].length];
for (int j = 0; j < original.length; j++) {
array[i][j] = original[j][i];
}
}
}
return array;
}
and then call it as:
System.out.print(Arrays.toString(test.minrow(transpose(array))));
Or, if you want to go without transpose, this is how you can do:
public static int[] mincol (int[][] n) {
int[] result = new int[n.length];
for (int i = 0; i < n.length; i++) {
int min = n[0][i];
for (int j = 0; j < n[0].length; j++) {
if (n[j][i] < min) {
min = n[j][i];
}
}
result[i] = min;
}
return result;
}
Your for loop looks ok. Check the code below I fixed some minor issues.
Based on your code replace Class code with below:
public class Test {
public static void main(String[] args) {
int[][]array={{6,1,9}, {0,1,2}, {3,7,5}};
int[] test;
test = minrow(array);
for(int i=0; i<test.length; i++){
System.out.println(test[i]);
}
}
public static int[] minrow(int[][] n){
int [] result = new int[n.length];
int min;
for(int i=0;i<n.length;i++){
min=0;
for(int j=0;j<n[i].length;j++){
if(n[i][j]<n[i][min]){
min=j;
}
}
result[i]=n[i][min];
}
return result;
}
}

two quick fix issues - Merge Sort

I have created my version of the merge sort algorithm in java code. My issues are these: when I run the code as is, I get a NullPointerExecpetion in the main on line 27 (see commented line). And I know there is way to make the method calls and instantiate newArray without them being static but Im not quite sure how.. can someone help fix these? I am still relatively new to java so be nice :)
Main:
import java.util.Random;
public class MergeSort_main
{
public static void main(String[] args)
{
int[] originalArray = new int[1000];
Random rand = new Random();
for (int i = 0; i < originalArray.length; i++)
{
int randNum = rand.nextInt(1000)+1;
originalArray[i] = randNum;
}
for(int i = 0; i < originalArray.length; i++)
{
System.out.println(i+"." + originalArray[i]);
}
System.out.println("---------------------End Random Array-------\n");
MergeSortAlgorithm.mergeSortAlg(originalArray);
int[] sortedArray = MergeSortAlgorithm.getSortedArray();
for(int i = 0; i < sortedArray.length; i++) //NULL POINTER EXCEPTION HERE
{
System.out.println(i+ "." + sortedArray[i]);
}
}
}
Algorithm Class:
public class MergeSortAlgorithm
{
private static int[] newArray;
public static void mergeSortAlg(int[] randomNums)
{
int size = randomNums.length;
if (size < 2)
{
return; //if the array can not be split up further, stop attempting to split.
}
int half = size / 2;
int firstHalfNums = half;
int secondHalfNums = size - half;
int[] firstArray = new int[firstHalfNums];
int[] secondArray = new int[secondHalfNums];
for (int i = 0; i < half; i++)
{
firstArray[i] = randomNums[i];
}
for (int i = half; i < size; i++)
{
secondArray[i - half] = randomNums[i];
}
mergeSortAlg(firstArray);
mergeSortAlg(secondArray);
merge(firstArray, secondArray, randomNums);
}
public static void merge(int[] firstArray, int[] secondArray, int[] newArray)
{
int firstHalfNums = firstArray.length;
int secondHalfNums = secondArray.length;
int i = 0; //iterator for firstArray
int j = 0; //iterator for second array
int k = 0; //interator for randomNums array
while (i < firstHalfNums && j < secondHalfNums)
{
if (firstArray[i] <= secondArray[j])
{
newArray[k] = firstArray[i];
i++;
k++;
}
else
{
newArray[k] = secondArray[j];
k++;
j++;
}
}
while (i < firstHalfNums)
{
newArray[k] = firstArray[i];
k++;
i++;
}
while (j < firstHalfNums)
{
newArray[k] = secondArray[j];
k++;
j++;
}
}
public static int[] getSortedArray()
{
return newArray;
}
}
Basically, the only problem with your code is that you don't initialize newArray with any values, resulting in a null.
You are also redefining newArray at the top of your merge function .
The problem is that newArray[] is never instantiated i.e. newArray reference is pointing to null. And, no change is made in the newArray so value or reference returned to main is null. And, then you are performing sortedArray.length where sorted array having a null value.
You have to make newArray[] point to randomNums[].

Categories