Write a generic array list class. Task Description Your goal for this lab is to write a generic ArrayList class similar to the one in the given lecture notes on array lists. The ArrayList Class The public constructors and methods required for the ArrayList class are listed here. The type E is the generic type of an element of the list. ArrayList() Construct an empty ArrayList object. int size() Return the size (number of items) in this ArrayList. boolean isEmpty() Return true if this ArrayList has no items. (This is the same as the size equal to zero.) Return false if the size is greater than zero. void add(E value) Add the given element, value, to the end of the list. void add(int index, E value) Add the given element, value, to the list at the given index. After this operation is complete, get(index) will return value. This operation is only valid for 0 <= index <= size(). E get(int index) Return the element of the list at the given index. This operation is only valid for 0 <= index < size(). This operation does not modify the list. E remove(E value) Removes and returns the first occurrence of the specified element from this list, if it is present. E remove(int index) Remove and return the element with the given index from the list. This operation is only valid for 0 <= index < size(). After this operation, all elements that had an index greater than index (as determined by get()) will have their index reduced by one. void clear() Removes all the elements from this list. Requirements 1. Your class must be named ArrayList. 2. Your class must provide the methods listed above for construction, accessing, and manipulating ArrayList objects. 3. Other than for testing purposes, your ArrayList class should do no input or output.
import java.util.Random;
public class MyArrayList<E> {
// YOUR CODE ANYWHERE IN THIS CLASS
private Object[] array;
public static final int CAPACITY = 4;
public int size;
public int capacity;
// there could be more instance variables!
/*
* initialise array with capacity. If capacity is less than 1, use CAPACITY.
*
*/
@SuppressWarnings("unchecked")
public MyArrayList() {
array = new Object[CAPACITY];
this.capacity = 4;
size = 0;
}
public MyArrayList(int capacity) {
array = new Object[capacity];
this.capacity = capacity;
size = 0;
}
public void add(int i, E val) {
if (i >= capacity)
return;
else
{
size++;
for (int x = size - 1; x > i; x--) {
array[x] = array[x - 1];
}
array[i] = val;
}
}
public void add(E x) {
if (size >= capacity){
Object[] temp = new Object[2*(array.length)];
for(int i=0;i<size;++i){
temp[i] = array[i];
}
this.capacity = 2*array.length;
array = temp;
array[size++] = x;
return;
}
else
{
array[size++] = x;
}
}
public void set(int i, E val) {
// YOUR CODE ANYWHERE IN THIS CLASS
if (i >= capacity)
return;
else
{
size++;
for (int x = size - 1; x > i; x--) {
array[x] = array[x - 1];
}
array[i] = val;
}
}
/*
* if index is outside range of array. return null
*
*/
public Object get(int i) {
// YOUR CODE ANYWHERE IN THIS CLASS
if (i >= capacity)
throw new ArrayIndexOutOfBoundsException();
else
return array[i];
}
/*
* if index is outside range of array. return null
*
*/
public void remove(int i) {
if (i >= size || size == 0)
return;
else {
Object e = array[i];
for (int x = i; x < this.array.length - 1; x++) {
array[x] = array[x + 1];
}
size--;
}
}
public void remove(E ob) {
// YOUR CODE ANYWHERE IN THIS CLASS
if (size == 0)
return;
else {
int i;
for (i = 0; i < size; ++i) {
if (ob.equals(array[i]))
break;
}
if (i == size)
return;
Object e = array[i];
for (int x = i; x < this.array.length - 1; x++) {
array[x] = array[x + 1];
}
size--;
}
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public boolean contains(E ob) {
for (int i = 0; i < size; ++i) {
if (ob.equals(array[i]))
return true;
}
return false;
}
public int search(Object ob) {
for (int i = 0; i < size; ++i) {
if (ob.equals(array[i]))
return i;
}
return -1;
}
public String toString() {
String res = "";
for (int i = 0; i < size; ++i) {
res = res + array[i] + " ";
}
return res;
}
@Override
public boolean equals(Object other) {
if (other == null)
return false;
System.out.println(this);
System.out.println((Integer) other);
if (other == this)
return true;
return false;
}
public static void main(String args[]){
MyArrayList<String> myArrayList = new MyArrayList<String>();
myArrayList.add("Rahul");
myArrayList.add("Chegg");
System.out.println(myArrayList.toString());
System.out.println("Size of String arrayList is: " + myArrayList.size );
myArrayList.add("Hello");
System.out.println("Size of String arrayList is: " + myArrayList.size );
for(int i=0;i<myArrayList.size;++i){
System.out.print(myArrayList.array[i] + " ");
}
System.out.println("\n");
MyArrayList<Integer> myArrayList1 = new MyArrayList<Integer>();
myArrayList1.add(1);
myArrayList1.add(2);
System.out.println(myArrayList1.toString());
System.out.println("Size of Integer arrayList is: " + myArrayList1.size );
}
}
========================================================
SEE OUTPUT

PLEASE COMMENT if there is any concern.
==========================================
Write a generic array list class. Task Description Your goal for this lab is to write...
Generic Linked Lists ( Program should be written in Java language). Modify the linked list class presented in this chapter so it works with generic types. Add the following methods drawn from the java.util.List interface: -void clear(): remove all elements from the list. -E get(int index): return the element at position index in the list. -E set(int index, E element): replace the element at the specified position with the specified element and return the previous element. Test your generic linked...
Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end. Here is what is missing : Here is my code: public class GSL { private String arr[]; private int size; public GSL() { arr = new String[10]; size = 0; } public int size() { return size; } public void add(String value) { ...
JAVA - Circular Doubly Linked
List
Does anybody could help me with this method below(previous)?
public E previous() {
// Returns the previous Element
return null;
}
Explanation:
We have this class with these two implemented
inferfaces:
The interfaces are:
package edu.ics211.h04;
/**
* Interface for a List211.
*
* @author Cam Moore
* @param the generic type of the Lists.
*/
public interface IList211 {
/**
* Gets the item at the given index.
* @param index the index....
PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is, if the list is full when an item is being added to this list, the elements will be moved to a larger array. The new array should have twice the size of the original array. Using the new class ArrayList, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should...
Please write in Java Recall that the ADT list class methods are; void List() bool isEmpty() int size() void add(int item, int pos)//inserts item at specified position (first postion is 1) void remove(int index)//removes item from specified position void removeAll() int indexOf(int item)//returns the index of item int itemAt(int index)//returns the item in position specified by index Implementation of LIST ADT operations and details are hidden. Only the methods listed above are...
package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic * collection to store elements where indexes represent priorities and the * priorities can change in several ways. * * This collection class uses an Object[] data structure to store elements. * * @param <E> The type of all elements stored in this collection */ // You will have an error until you have have all methods // specified in interface PriorityList included inside this...
Write a Java program to work with a generic list ADT using a fixed size array, not ArrayList. Create the interface ListInterface with the following methods a) add(newEntry): Adds a new entry to the end of the list. b) add(newPosition, newEntry): Adds a new entry to the list at a given position. c) remove(givenPosition): Removes the entry at a given position from the list. d) clear( ): Removes all entries from the list . e) replace(givenPosition, newEntry): Replaces the entry...
I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...
I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item) Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...
Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> { // Data fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; @SuppressWarnings("unchecked") public KWArrayList() { capacity...