Question

Modify the existing ArrayLists's contents, by erasing the second element, then inserting 100 and 102 in...

Modify the existing ArrayLists's contents, by erasing the second element, then inserting 100 and 102 in the shown locations. Use ArrayList's remove() and add() only. Sample ArrayList content of below program:

100 101 102 103 

import java.util.ArrayList;

public class ArrayListADT {

public static void main (String [] args) {
ArrayList<Integer> numsList = new ArrayList<Integer>();
int numOfElem = 4;

numsList.add(101);
numsList.add(200);
numsList.add(103);


  /* Your solution goes here */
   numsList.remove(1);
numsList.add(0, 100);
numsList.add(2, 102);
printArray(numsList, numOfElem);
return;

}
}

Fix the error.

ArrayListADT.java:21: error: cannot find symbol

printArray(numsList, numOfElem);

^

symbol: method printArray(ArrayList<Integer>,int)

location: class ArrayListADT

1 error

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;

public class ArrayListADT {

    public static void main(String[] args) {
        ArrayList<Integer> numsList = new ArrayList<Integer>();
        int numOfElem = 4;

        numsList.add(101);
        numsList.add(200);
        numsList.add(103);

        numsList.remove(1);
        numsList.add(0, 100);
        numsList.add(2, 102);
        printArray(numsList, numOfElem);
        return;

    }

    public static void printArray(ArrayList<Integer> numbers, int size) {
        for (int i = 0; i < numbers.size(); i++) {
            System.out.print(numbers.get(i) + " ");
        }
        System.out.println();
    }
}

Add a comment
Know the answer?
Add Answer to:
Modify the existing ArrayLists's contents, by erasing the second element, then inserting 100 and 102 in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Directions: Modify the existing ArrayLists's contents, by erasing the second element, then inserting 100 and 102...

    Directions: Modify the existing ArrayLists's contents, by erasing the second element, then inserting 100 and 102 in the shown locations. Use ArrayList's remove() and add() only. Sample ArrayList content of below program: 100 101 102 103 /* below code is given */ import java.util.ArrayList; public class ArrayListADT { public static void main (String [] args) { ArrayList<Integer> numsList = new ArrayList<Integer>(); int numOfElem = 4; numsList.add(101); numsList.add(200); numsList.add(103); /* Your solution goes here */ } }

  • Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args)...

    import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...

  • Having trouble with my code, it keeps giving me an error every time I run it....

    Having trouble with my code, it keeps giving me an error every time I run it. Can someone please help me understand what I'm doing wrong? *********CODE*************** // Java program to read the file contents, sort it and output the sorted content to another file import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; public class Datasort { public static void main(String[] args) throws IOException { File fin = new File("input.txt");...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Rules of implementation!: You may NOT modify any of the files except Expression.java in ANY way....

    Rules of implementation!: You may NOT modify any of the files except Expression.java in ANY way. You may NOT make ANY modifications to Expression.java EXCEPT: Write in the bodies of the methods you are asked to implement, Add private helper methods as needed (including the recursive evaluate method discussed below.) Note that the java.io.*, java.util.*, and java.util.regex.* import statements at the top of the file allow for using ANY class in java.io, java.util, and java.util.regex without additional specification or qualification....

  • Implement a Java program using simple console input & output and logical control structures such that...

    Implement a Java program using simple console input & output and logical control structures such that the program prompts the user to enter 5 integer scores between 0 to 100 as inputs and does the following tasks For each input score, the program determines whether the corresponding score maps to a pass or a fail. If the input score is greater than or equal to 60, then it should print “Pass”, otherwise, it should print “Fail”. After the 5 integer...

  • Please Modify TestPart2 to test the correctness and efficiency of FasterDefaultList. Thanks import java.util.List; import java.util.AbstractList;...

    Please Modify TestPart2 to test the correctness and efficiency of FasterDefaultList. Thanks import java.util.List; import java.util.AbstractList; import java.util.Map; import java.util.HashMap; public class DumbDefaultList<T> extends AbstractList<T> { Map<Integer,T> map; public DumbDefaultList() { map = new HashMap<Integer,T>(); } public int size() { return Integer.MAX_VALUE; } public T get(int i) { return map.get(i); } public T set(int i, T x) { return map.put(i, x); } public void add(int i, T x) { Map<Integer, T> map2 = new HashMap<Integer,T>(); for (Integer k : map.keySet())...

  • 1.The following statement gets an element from position 4 in an array named myArray: x =...

    1.The following statement gets an element from position 4 in an array named myArray: x = myArray[4]; What is the equivalent operation using an array list named list.? A x = list.get(); B x = list.get(4); C x = list.get[4]; D x = list[4]; 2.Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 &&...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT