Question: Java Coding Question: Create class called matching with following method: public static int[] matching(int[] match, int[] mainArray) The method returns an array of index values
Java Coding Question:
Create class called "matching" with following method: public static int[] matching(int[] match, int[] mainArray) The method returns an array of index values when the match array is found in the main array. The first value of the return array must be how many matches there are. If there is no match method returns an array of 0
This is what should happen when you run the following code: int[] mainArray = {5,1,5,6,7,5,6,8,5,5,5}; int[] match1 = {5}; int[] match2 = {5,6}; int[] match3 = {5,6,7}; int[] a1 = matching(match1, mainArray); int[] a2 = matching(match2, mainArray); int[] a3 = matching(match3, mainArray);
The output for array a1 is: [6,0,2,5,8,9,10]
The output for array a2 is: [2,2,5]
The output for array a3 is: [1,2]
Remember the first digit of the output array is how many matches there are.
Asking this question again because the first person gave answer with many problems.
Please make sure the code works with different lengths and gives required output.
Here is the first answerers code. Maybe you can modify it to fix it or start from scratch
Thanks in advance for help. Absolutely no more classes or imports can be added.
public class Main
{
public static int[] matching (int match[], int mainArray[])
{
//count number of matching
int count = 0;
//length of mainArray
int len1 = mainArray.length;
//length of match
int len2 = match.length;
//new array for output that stores index and count
int[] index = new int[mainArray.length];
int k = 0;
for (int i = 0; i < len1 - 1; i++)
{
//if length of match array is 1 directly add index
if (len2 == 1)
{
if (mainArray[i] == match[0])
{
count++;
index[k++] = i;
}
}
//if len2 is not equal to 1 and match for consecutive numbers
if (mainArray[i] == match[0])
{
for (int j = 1; j < len2; j++)
{
if (mainArray[i + j] == match[j])
{
if (j == len2 - 1)
{
//increment count
count++;
//add index to index array
index[k++] = i;
//incerment after match index
i = i + len2 - 1;
}
}
}
}
}//for last element checking
if (len2 == 1)
{
if (mainArray[len1 - 1] == match[0])
{
count++;
index[k++] = len1 - 1;
}
}
//shift elements to right and add count to the index 0
int temp = count;
for (int i = count; i > 0; i--)
{
index[i] = index[i - 1];
}
index[0] = temp;
//return array index
return index;
}
public static void main (String[]args)
{
int[] mainArray = { 1, 2};
int[] match1 = { 1 };
int[] match2 = { 1, 2 };
int[] match3 = { 1, 2, 3 };
int[] a1 = matching (match1, mainArray);
int[] a2 = matching (match2, mainArray);
int[] a3 = matching (match3, mainArray);
//display result
System.out.print ("The output for array a1 is: [");
for (int i = 0; i < a1.length; i++)
{
if (a1[i] != 0 && i != a1[0])
System.out.print (a1[i] + ",");
if (a1[0] == i)
System.out.print (a1[i]);
}
System.out.print ("]");
System.out.println ();
System.out.print ("The output for array a2 is: [");
for (int i = 0; i < a2.length; i++)
{
if (a2[i] != 0 && i != a2[0])
System.out.print (a2[i] + ",");
if (a2[0] == i)
System.out.print (a2[i]);
}
System.out.print ("]");
System.out.println ();
System.out.print ("The output for array a3 is: [");
for (int i = 0; i < a3.length; i++)
{
if (a3[i] != 0 && i != a3[0])
System.out.print (a3[i] + ",");
if (a3[0] == i)
System.out.print (a3[i]);
}
System.out.print ("]");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
