Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello. I need some help with my Java programming homework. We have to replicate

ID: 641457 • Letter: H

Question

Hello. I need some help with my Java programming homework. We have to replicate java's array list data structure and cannot import it from the library. One of the required methods is to have one that will allow us to get a value from within a given location in the Arraylist. Another is we need a method that will check to see if the Arraylist is empty. Please include some commentary within your answer. Any help with this would be very appreciated. And I will rate your answer with 5 stars. Thanks Hello. I need some help with my Java programming homework. We have to replicate java's array list data structure and cannot import it from the library. One of the required methods is to have one that will allow us to get a value from within a given location in the Arraylist. Another is we need a method that will check to see if the Arraylist is empty. Please include some commentary within your answer. Any help with this would be very appreciated. And I will rate your answer with 5 stars. Thanks

Explanation / Answer

Hope it may helps you......

import java.util.ArrayList;
import java.util.Collections;

public class CopyElementsOfArrayListToArrayListExample {

public static void main(String[] args) {

//create first ArrayList object
ArrayList arrayList1 = new ArrayList();

//Add elements to ArrayList
arrayList1.add("1");
arrayList1.add("2");
arrayList1.add("3");

//create another ArrayList object
ArrayList arrayList2 = new ArrayList();

//Add elements to Arraylist
arrayList2.add("One");
arrayList2.add("Two");
arrayList2.add("Three");
arrayList2.add("Four");
arrayList2.add("Five");

/*
To copy elements of one Java ArrayList to another use,
static void copy(List dstList, List sourceList) method of Collections class.

This method copies all elements of source list to destination list. After copy
index of the elements in both source and destination lists would be identical.

The destination list must be long enough to hold all copied elements. If it is
longer than that, the rest of the destination list's elments would remain
unaffected.
*/

System.out.println("Before copy, Second ArrayList Contains : " + arrayList2);

//copy all elements of ArrayList to another ArrayList using copy
//method of Collections class
Collections.copy(arrayList2,arrayList1);

/*
Please note that, If destination ArrayList object is not long
enough to hold all elements of source ArrayList,
it throws IndexOutOfBoundsException.
*/

System.out.println("After copy, Second ArrayList Contains : " + arrayList2);
}
}