Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this Java Assignment, I need to write a subprogram that is given an array of integers and a given integer to search for. Scan

In this Java Assignment, I need to write a subprogram that is given an array of integers and a given integer to search for. Scan the array for matches with the given integer and create an array of the indexes with the matching entries. To store the indexes, start with an array of the same size, but, after finishing the scan, allocate an array of just the right size, copy the contents, and return the (potentially) smaller array.

For example, given the following: search array [ 101,103, 101, 114, 104, 105, 107 ] for 101 return the following: [ 0, 2, 5 ] (an array of exactly three elements). If there are no matches, return null. I wrote a code but it showing multiple error messages.

import java. util.Scanner;

import java. util.Arrays;

import java. util.ArrayList;

public class numberArray

{

public static void main(string args[])

{

Scanner console = new Scanner(System.in);

ArrayList data = new ArrayList<>();

int[] array1 = { 101, 103, 101, 104, 105, 101, 107 };

int[] array2 = array1; //reference copy

System.out.print("The array1 has ");

for (int ix = 0; ix < 7; ix++)

{

if (ix < 4)

System.out.print(array1[ix] + ", " );

else

System.out.println(array1[ix] + ". " );

}//end of for

System.out.print("The array2 has ");

for (int ix = 0; ix < 7; ix++)

{

array2[ix] = array2[ix] + ix;

if (ix < 4)

System.out.print(array2[ix] + ", " );

else

System.out.println(array2[ix] + ". " );

}//end of for

//Prove "int[] array2 = array1;" is reference copy!

System.out.print("The array1 has ");

for (int ix = 0; ix < 7; ix++)

{

if (ix < 4)

System.out.print(array1[ix] + ", " );

else

System.out.println(array1[ix] + ". " );

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions