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...
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...
java method for dequeue write the “dequeue” method for a queue of type double. If the...
java method for dequeue write the “dequeue” method for a queue of type double. If the queue is empty return 0.0. Make sure to change the links properly ensure that the data structure remains a queue. use the code provided below public class Q04 { public class ListNode//Public for testing purposes { public double data; public ListNode link; public ListNode(double aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//Public for testing purposes public ListNode...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT