bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 7, Problem 10E

Explanation of Solution

Method definition for “remove”:

/* Method definition for "remove" */

public static int[] remove(int v, int[] in)

{     

    /* Initializes the array count to "0"*/

    int arrayCount = 0;

/* Compute the number of elements that will be in the result */

    for(int index = 0; index < in.length; index++)

    {

/* If the value of "in[index]" is not equal to "v", then */

        if(in[index] != v)

            /* Increment the array count */

            arrayCount++;

    }

/* Declare the variable "newArray" for copy the array into new array */

    int newArray[] = new int[arrayCount];

    /* Initializes the array position to "0" */

    int arrayPosition = 0;

/* Copy the new array values into "newArray" using "for" loop */

    for(int index = 0; index < in.length; index++)

    {

        if(in[index] != v)

        {

            newArray[arrayPosition] = in[index];

            arrayPosition++;

        }

    }

    //Finally returns the new array values

    return newArray;

}

Complete code:

The complete code for implementing “remove” method is given below:

//Define "removeArrayTest" class

public class removeArrayTest

{

    //Define main function

    public static void main(String[] args)

    {

        /* Initializes the array "arrValues" */

        int[] arrValues = {0, 1, 3, 2, 3, 0, 3, 1};

        //Display given statement

System.out.println("New Array of integers after removing element '3': ");

/* Call "remove" method and store the result into "resArray" */

        int[] resArray = remove(3, arrValues);

        /* Display the elements of "resArray" */

for(int index = 0; index < resArray.length; index++)

        {

            System.out.print(resArray[index] + " ");

        }

        System.out.println();     

    }

    /* Method definition for "remove" */

    public static int[] remove(int v, int[] in)

    {     

        /* Initializes the array count to "0"*/

        int arrayCount = 0;

/* Compute the number of elements that will be in the result */

        for(int index = 0; index < in...

Blurred answer
Students have asked these similar questions
Need Java method please. Thank you.
Need Java method please. Thank you.
3. Write two nested loops to generate the following output. (Note: There is one space between each number, and any extra line shown is intentional.) 12 10 8 6 18 15 12 24 20 30 2 3 3 6 48 12 5 10 15 20 6 12 18 24 30

Chapter 7 Solutions

Java: An Introduction to Problem Solving and Programming plus MyProgrammingLab with Pearson eText -- Access Card Package (7th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License