
iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of the arraylist
vii.remove all the elements at any odd index of the original arraylist (not the cloned one)
viii.print out the original arraylist ix.reverse the cloned arraylist x.print out the cloned arraylist (this arraylist should still contain the original sequence of elements in order)
xi.merge the cloned arraylist to the original arraylist (please think about what happens and draw a diagram for yourself to visualize it.)
xii.print out the original arraylist
public class MyArrayList implements Cloneable{
public Object[] obj;
public int maxsize = 100,initialsize = 0;
public MyArrayList(){
obj = new Object[maxsize];
}
public void append(Object element){
if(initialsize < maxsize)
obj[initialsize++] = element;
else
System.out.println("Full");
}
public void clear(){
obj = new Object[maxsize];
}
public void contains(Object element){
int flag = 1;
for(int i=0;i<initialsize;i++){
if(obj[i] == element){
System.out.println("Found");
flag = 1;
break;
}
}
if(flag == 0)
System.out.println("Not
Found");
}
public Object elementAt(int index){
return obj[index];
}
public int indexOf(Object element){
for(int i=0;i<initialsize;i++)
{
if(obj[i] == element)
return i;
}
return -1;
}
public void insertAt(int index,Object element){
for(int i=initialsize;i>index;i--){
obj[i] = obj[i-1];
}
obj[index] = element;
}
public boolean isEmpty(){
if(initialsize == 0)
return true;
return false;
}
public void removedAt(int index){
for(int i=index;i<initialsize-1;i++){
obj[i] = obj[i+1];
}
}
public void remove(Object element){
for(int i=0;i<initialsize;i++){
if(obj[i] == element){
removedAt(i);
break;
}
}
}
public void replace(int index,Object element){
obj[index] = element;
}
public int size(){
return initialsize;
}
public boolean ensureCapacity(int minCapacity){
if(initialsize >= minCapacity)
return true;
return false;
}
public Object clone() throws CloneNotSupportedException{
return (MyArrayList) super.clone();
}
public void removeRange(int fromIndex,int toIndex){
for(int i=fromIndex;i<toIndex;i++){
removedAt(i);
}
}
public String toString(){
String res = " ";
for(int i=0;i<initialsize;i++){
res = res + str(obj[i]);
}
return res;
}
public void reverse(){
int last = initialsize -1;
for(int i=0;i<(initialsize-1)/2;i++){
Object temp = obj[i];
obj[i] = obj[last];
obj[last] = temp;
last--;
}
}
public void merge(MyArrayList arraylist2){
for(int i=0;i<arraylist2.size();i++){
obj[initialsize++] =
arraylist2[i];
}
}
public void print(){
for(int i=0;i<initialsize;i++)
System.out.print(obj[i] + "
");
}
}
This above will be the Abstract data type.
Now for testing,
public Class Lab2{
public static void test(){
MyArrayList list = new
MyArrayList();
list.append(0);
list.append(1);
int one = 0,two =1;
for(int i=0;i<23;i++){
int third = one
+ two;
list.append(third);
one = two;
two =
third;
}
list.print();
list.reverse();
list.print();
MyArrayList list2 =
(MyArrayList)list.clone();
list.remove(3);
list.print();
list2.reverse();
list2.print();
list.merge(list2);
list.print();
}
}
Let this one helps Thank you:)
iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of...
I need some help doing this Java project. 1.Implement the MySort class with the following operations. a.bubbleSort(MyArrayList arraylist) – conduct bubble sort on the elements contained in a MyArrayList object. b.selectionSort(MyArrayList arraylist) – conduct selection sort on the elements contained in a MyArrayList object. 2.Implement the MySearch class with the following operations. a.binarySearch(MyArrayList arraylist, Comparable target) – conduct binary search on sorted elements contained in a MyArrayList object. b.linearSearchSorted (MyArrayList arraylist, Comparable target) – conduct linear search on sorted elements...
3. Create a program named TestArrayList, and declare an ArrayList with an initial capacity of 10. Insert one copy of the string "Python", five copies of the string "Java" followed by four copies of the string "C++" so that your vector is now filled to сараcity. Add a class method named printArrayList to print out all the elements stored within an ArrayList on separate lines. Test your method to be sure it works a) Add a class method named delete...
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...
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()...
JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...
I have a text file that this project needs to read from that has every possible combination of words but I am unsure how to do it. Also I'm pretty sure this program could use a program called "myArraylist" to sort through the text file but I am unsure how to implement this. I am unsure how to show the text file because it is massive, and I don't think it is possible to upload a text file to chegg....
Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...
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....
-Fill in the reverse method below to return a new DoublyLinkedList consisting of the same elements in reverse order. -The reverse method must not modify the original DoublyLinkedList. -The reverse method must run in linear time. Can someone answer this for me, please? In Java public class DoublyLinkedList { int size; Node firstNode; Node lastNode; public DoublyLinkedList() { size = 0; firstNode = null; lastNode = null; } // Problem 1 (15 pts) // Fill in the method below to...