Question

In: Computer Science

Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList...

Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList with a public constructor that initializes the list using a passed array of Object references. Assert that the passed array is not null. Next, implement:

1)Object get(int), which takes an int index and returns the Object at that index

2)void set(int, Object), which takes an int index and an object reference and sets that value at the index to the passed reference

Both your get and set method should assert that index passed is valid for that SimpleArrayList.

Here's an example how your code should work

SimpleArrayList list = new SimpleArrayList(new int[] {1,2,5});

System.out.println(list.get(1)); //prints 2 list.set(1,6);

System.out.println(list.get(1)); //prints 6

System.out.println(list.get(0)); //prints 1

System.out.println(list.get(4)); //generates an assertion error

Note: shouldn't be using java.util.List for the question

Solutions

Expert Solution

Here's the Java Code for the problem asked:

package javaProject;

// generic class SimpleArrayList with template T
class SimpleArrayList<T> {
        
        // private array of type T
        private T[] arr;
        
        // constructor
        public SimpleArrayList(T[] list) {
                assert list != null : "Array is NULL";
                arr = list;
        }
        
        // getter method
        public T get(int index) {
                assert (index >= 0 && index < arr.length) : "Index Out of Bounds";
                return arr[index];
        }
        
        // setter method
        public void set(int index, T item) {
                assert (index >= 0 && index < arr.length) : "Index Out of Bounds";
                arr[index] = item;
        }
        
        
}

public class ArrayImple {

        // driver function
        public static void main(String[] args) {

                SimpleArrayList<Integer> list = new SimpleArrayList<Integer>(new Integer[] {1, 2, 5});

                System.out.println(list.get(1)); //prints 2 
                
                list.set(1,6);

                System.out.println(list.get(1)); //prints 6

                System.out.println(list.get(0)); //prints 1

                System.out.println(list.get(4)); //generates an assertion error
        }

}

Added comments in the code wherever necessary, do refer them for better understanding! Also, for any doubt, feel free to reach back :)

Sample Output:

Hope it helps, consider hitting a like if it did :)

Cheers!


Related Solutions

Multi-Dimensional Arrays Create main class named MultiArray.    Create a method which outputs (prints) all the values...
Multi-Dimensional Arrays Create main class named MultiArray.    Create a method which outputs (prints) all the values in the array. Call this method from each of the other methods to illustrate the data in the array. Note: Be sure to populate, and update the values in the multi-array, and then print the contents of the multi-array (Don't just print the design patterns.) The goal is to practice navigating the multi-array. Declare a multi-dimensional array of integers, which is 10 rows of...
JAVA How to make a graph class that uses a linked list class to store nodes...
JAVA How to make a graph class that uses a linked list class to store nodes and linked list within each node to store adjacency list The linked list class has been made already.   import java.util.*; public class LinkedList implements Iterable { private int size = 0; private Node head; private Node tail; private class Node { private T data; private Node prev; private Node next; public Node(T data) { this.data = data; } }    public Iterator iterator() {...
Create an unsorted LIST class. Each list should be able to store 100 names.
Create an unsorted LIST class. Each list should be able to store 100 names.
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array.
     program will enter data into two single dimension arrays (do not store duplicate values in arrays)...
     program will enter data into two single dimension arrays (do not store duplicate values in arrays)      program will find the union and intersection of the two arrays using one function      program will find the symmetric difference of two arrays      program will display the union, intersection, and symmetric difference   */     short* input_data(short size);   // function to dynamically allocate and array and enter data into the array void display_data(short *data, short size); // function to display data in an array void...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue...
PYTHON- create a queue class that uses a LINKEDLIST in order to store data. this queue will call on a linkedlist class class Queue: def __init__(self): self.items = LinkedList() #has to be O(1) def enqueue(self, item): #has to be O(1) def dequeue(self): def is_empty(self): def __len__(self):
*Create a python function that uses a while list to allow a user to enter values...
*Create a python function that uses a while list to allow a user to enter values for sales and to add each value into a list in order to track all values entered. Set the gathering of values to stop when the value of zero is entered. Then take the created list and calculate the values within the list to return the total for all the values. Next, take the previously created list and display all the values from smallest...
how do I create a graph class to store a adjacency list representation of a graph?...
how do I create a graph class to store a adjacency list representation of a graph? I need some help and dont conpletely understand how to start. if you could provide a code with some steps id appreciate it.
Complete the implementation of the Die class. Note that the Die class uses an array to...
Complete the implementation of the Die class. Note that the Die class uses an array to represent the faces of a die. You need to complete the no-argument constructor and the two methods compareTo and equals. Code: import java.util.Arrays; import java.util.Objects; import java.util.Random; /* * NOTE TO STUDENTS: * The constructor that you need to complete can be found on line 47. * * The two methods you need to complete can be found at the end of this file....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT