Question

In: Computer Science

java 1.) a method to append a 2d double array at the right of another 2d...

java

1.) a method to append a 2d double array at the right of another 2d double array, return a new array.

E.g. numss1: {{1, 2}, {3, 4, 5}, {6}}, numss2: {{7}, {8, 9}}, return {{1, 2, 7}, {3, 4, 5, 8, 9}, {6}}

Solutions

Expert Solution

The required method is given below:

//method to append array
public static int[][] appendArray(int arr1[][], int arr2[][])
{
int maxCol=0;
  
for(int i=0; i<arr1.length; i++)
{
if(i<arr2.length)
{
if((arr1[i].length+arr2[i].length) > maxCol)
{
maxCol = arr1[i].length+arr2[i].length;
}
}
else
{
if((arr1[i].length) > maxCol)
{
maxCol = arr1[i].length;
}
}
}
  
int newArr[][] = new int[arr1.length][maxCol];
  
for (int i = 0; i < arr1.length; i++)
{
int j;
for (j = 0; j < arr1[i].length; j++)
{
newArr[i][j] = arr1[i][j];
}
if(i<arr2.length)
{
for (int k=0;k < arr2[i].length; k++)
{
newArr[i][j+k] = arr2[i][k];
}
}
}
  
return newArr;
}

The complete program source code including the above method for testing is given below:


public class Main
{
//method to append array
public static int[][] appendArray(int arr1[][], int arr2[][])
{
int maxCol=0;
  
for(int i=0; i<arr1.length; i++)
{
if(i<arr2.length)
{
if((arr1[i].length+arr2[i].length) > maxCol)
{
maxCol = arr1[i].length+arr2[i].length;
}
}
else
{
if((arr1[i].length) > maxCol)
{
maxCol = arr1[i].length;
}
}
}
  
int newArr[][] = new int[arr1.length][maxCol];
  
for (int i = 0; i < arr1.length; i++)
{
int j;
for (j = 0; j < arr1[i].length; j++)
{
newArr[i][j] = arr1[i][j];
}
if(i<arr2.length)
{
for (int k=0;k < arr2[i].length; k++)
{
newArr[i][j+k] = arr2[i][k];
}
}
}
  
return newArr;
}
  
public static void display(int arr[][])
{
System.out.println("\nThe 2D array is: ");
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++)
{
if(arr[i][j]!=0)
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
  
  
public class Main
{
//method to append array
public static int[][] appendArray(int arr1[][], int arr2[][])
{
int maxCol=0;
  
for(int i=0; i<arr1.length; i++)
{
if(i<arr2.length)
{
if((arr1[i].length+arr2[i].length) > maxCol)
{
maxCol = arr1[i].length+arr2[i].length;
}
}
else
{
if((arr1[i].length) > maxCol)
{
maxCol = arr1[i].length;
}
}
}
  
int newArr[][] = new int[arr1.length][maxCol];
  
for (int i = 0; i < arr1.length; i++)
{
int j;
for (j = 0; j < arr1[i].length; j++)
{
newArr[i][j] = arr1[i][j];
}
if(i<arr2.length)
{
for (int k=0;k < arr2[i].length; k++)
{
newArr[i][j+k] = arr2[i][k];
}
}
}
  
return newArr;
}
  
public static void display(int arr[][])
{
System.out.println("\nThe 2D array is: ");
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++)
{
if(arr[i][j]!=0)
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
  
   public static void main(String[] args)
   {
       int numss1[][] = {{1, 2}, {3, 4, 5}, {6}};
       int numss2[][] = {{7}, {8, 9}};
      
       display(numss1);
      
       display(numss2);
      
       int newArr[][] = appendArray(numss1, numss2);
      
       display(newArr);
   }
}

OUTPUT:


The 2D array is:
1 2
3 4 5
6

The 2D array is:
7
8 9

The 2D array is:
1 2 7
3 4 5 8 9
6


Related Solutions

java 2D array / recursion explain every method You are requested to write a Java program...
java 2D array / recursion explain every method You are requested to write a Java program of a simple Memory Management Unit. The program should allow the following: 1. The user can create a process asking for memory. The program will return a process ID if the requested memory can be allocated. It will also print the allocated Base and Limit. 2. The user can delete a process by specifying a process ID. The program should do that and free...
1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes...
1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes of the largest value stored in the 2D array. Specific requirements: (1) Your main function defines the 2D array a. You will need to prompt the user to specify the size of the 2D array b. You will need to prompt the user to put in numbers to initialize the array (2) Write a function to display the array that is visualized as rows...
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
i want a program in java that finds the shortest path in a 2D array with...
i want a program in java that finds the shortest path in a 2D array with obstacles from source to destination using BFS and recursion. The path must be stored in a queue. The possible moves are left,right,up and down.
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Consider the append() operation for a Dynamic Array. In the best case, the operation is O(1)....
Consider the append() operation for a Dynamic Array. In the best case, the operation is O(1). This corresponds to the case where there was room in the space we have already allocated for the array. However, in the worst case, this operation slows down to O(n). This corresponds to the case where the allocated space was full and we must copy each element of the array into a new (larger) array. This problem is designed to discover runtime bounds on...
Using Java: Implement print2D(A) so that it prints out its 2D array argument A in the...
Using Java: Implement print2D(A) so that it prints out its 2D array argument A in the matrix form. Implement add2Ds(A,B) so that it creates and returns a new 2D array C such that C=A+B. Implement multiScalar2D(c,A) so that it creates and returns a new 2D array B such that B=c×A. Implement transpose2D(A), which creates and returns a new 2D array B such that B is the transpose of A. Your output should look like the following:A= 1234 5678 9 10...
Java- Create a new class named Forest that has a 2D array of integers to store...
Java- Create a new class named Forest that has a 2D array of integers to store a forest. Create a private inner class Position to store the row and column of the current cell. Create a Depth First Search Method that uses a stack to remove the current tree and search its neighbors based on the following pseudocode: // given a forest "f" // store positions on fire Stack<Position> cellsToExplore // the forest is aflame! for each tree in the...
Describe how Java creates a single dimension array and a double dimension array. Describe and explain...
Describe how Java creates a single dimension array and a double dimension array. Describe and explain how Java processes the contents of a single dimension array. Describe and explain how Java saves values to a single dimension array, and to a double dimension array.
1) Design and implement a method to double all elements of an array. Important: You don’t...
1) Design and implement a method to double all elements of an array. Important: You don’t need to test test method in main(). You don’t need to initialize the array. pubilc static void doubleElements(int [] arr) { } 2) Start with the code below and complete the getInt method. The method should prompt the user to enter an integer. Scan the input the user types. If the input is not an int, throw an IOException; otherwise, return the int. In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT