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...
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 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() {...
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.
*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...
Arrays provide the programmer the ability to store a group of related variables in a list...
Arrays provide the programmer the ability to store a group of related variables in a list that can be accessed via an index. This is often very useful as most programs perform the same series of calculations on every element in the array. To create an array of 10 integers (with predefined values) the following code segment can be utilised: int valueList[10] = {4,7,1,36,23,67,61,887,12,53}; To step through the array the index '[n]' need to be updated. One method of stepping...
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
Python programming Summary Store the times into arrays called Chevy[ ] and Ford[ ]. Then list...
Python programming Summary Store the times into arrays called Chevy[ ] and Ford[ ]. Then list the winner of each pair, giving the number of seconds the winner won by. At the end declare which team won based on which team had the most wins. Lab Steps There are eight cars in each team called Chevy and Ford. One car from each team races its opponent on the drag strip. Read in the racing times for the eight Chevy cars...
Below is a definition of the class of a simple link list. class Chain; class ChainNode...
Below is a definition of the class of a simple link list. class Chain; class ChainNode { friend class Chain; private: int data; ChainNode *link ; }; class Chain{ public: ... private: ChainNode *first; // The first node points. } Write a member function that inserts a node with an x value just in front of a node with a val value by taking two parameters, x and val. If no node has a val value, insert the node with...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The class should have one static methods: public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false. NOTE: You are not allowed to use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT