Question:
Java: ArrayList help?
jumba
2007-05-15 16:06:06 UTC
Create an object that has a getName(), setName(), and toString(). Create an ArrayList of 7 elements and test what each of the following methods do: int size(), boolean add (E element),E get(int index), E set(int index, E element), void add(int index, E element), and E remove(int index). Constructed an ArrayList of an element(object) with at least an intial size of 7.
int size(): prints size of array whenever size changes.
boolean add (E element): uses this add to add an element and uses the boolean return value.
E get(int index): uses get() to get a certain element by its index and output information about it using its toString().
E set(int index, E element): used set() to replace an element at a certain index with another & output information about displaced element using its toString().
void add(int index, E element): uses this add to add an element at a certain index
E remove(int index): used remove to remove an element at a certain index & prints name of removed element.

Thanks
Five answers:
2007-05-15 16:21:50 UTC
I hate Java. You can look it up in your book, just like the rest of us.
Shaifu
2007-05-15 16:58:52 UTC
Would suggest you that for Java Program examples see this

http://www.idevelopment.info/data/Programming/java/PROGRAMMING_Java_Programming.shtml

************************************

First Program :

************************************

class Name

{

private String name;

public Name()

{

name=new String("Jumba");

}

public Name(String N)

{

name=new String(N);

}

public String getName()

{

return name;

}

public void setName(String N)

{

name=N;

}

public String toString()

{

return name;

}



}

class MainClass

{

public static void main(String args[])

{

Name N=new Name();

System.out.println(N.toString());

N.setName("Jumba1");

System.out.println(N.getName());

}

}

***************************************************

IInd Program :

**************************************************

public class ArrayListExample {





/**

* Provides an example of how to work with the ArrayList container.

*/

public void doArrayListExample() {



final int MAX = 10;

int counter = 0;



System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Create/Store objects in an ArrayList container. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



List listA = new ArrayList();

List listB = new ArrayList();



for (int i = 0; i < MAX; i++) {

System.out.println(" - Storing Integer(" + i + ")");

listA.add(new Integer(i));

}



System.out.println(" - Storing String(Alex)");

listA.add("Alex");



System.out.println(" - Storing String(Melody)");

listA.add("Melody");



System.out.println(" - Storing String(Jeff)");

listA.add("Jeff");



System.out.println(" - Storing String(Alex)");

listA.add("Alex");



System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Retrieve objects in an ArrayList container using an Iterator. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



Iterator i = listA.iterator();

while (i.hasNext()) {

System.out.println(i.next());

}





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Retrieve objects in an ArrayList container using a ListIterator. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



counter = 0;

ListIterator li = listA.listIterator();

while (li.hasNext()) {

System.out.println("Element [" + counter + "] = " + li.next());

System.out.println(" - hasPrevious = " + li.hasPrevious());

System.out.println(" - hasNext = " + li.hasNext());

System.out.println(" - previousIndex = " + li.previousIndex());

System.out.println(" - nextIndex = " + li.nextIndex());

System.out.println();

counter++;

}





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Retrieve objects in an ArrayList container using index. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



for (int j=0; j < listA.size(); j++) {

System.out.println("[" + j + "] - " + listA.get(j));

}





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Search for a particular Object and return its index location. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



int locationIndex = listA.indexOf("Jeff");

System.out.println("Index location of the String \"Jeff\" is: " + locationIndex);





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Search for an object and return the first and last (highest) index. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



System.out.println("First occurance search for String \"Alex\". Index = " + listA.indexOf("Alex"));

System.out.println("Last Index search for String \"Alex\". Index = " + listA.lastIndexOf("Alex"));





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Extract a sublist from the main list, then print the new List. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



List listSub = listA.subList(10, listA.size());

System.out.println("New Sub-List from index 10 to " + listA.size() + ": " + listSub);





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Sort the Sub-List created above. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



System.out.println("Original List : " + listSub);

Collections.sort(listSub);

System.out.println("New Sorted List : " + listSub);

System.out.println();





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Reverse the Sub-List created above. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



System.out.println("Original List : " + listSub);

Collections.reverse(listSub);

System.out.println("New Reversed List : " + listSub);

System.out.println();





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Check to see if the Lists are empty. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



System.out.println("Is List A empty? " + listA.isEmpty());

System.out.println("Is List B empty? " + listB.isEmpty());

System.out.println("Is Sub-List empty? " + listSub.isEmpty());





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Clone the initial List. |");

System.out.println("| NOTE: The contents of the List are object references, so both |");

System.out.println("| of the List's contain the same exact object reference's. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



System.out.println("List A (before) : " + listA);

System.out.println("List B (before) : " + listB);

System.out.println("Sub-List (before) : " + listSub);

System.out.println();

System.out.println("Are List's A and B equal? " + listA.equals(listB));

System.out.println();

listB = new ArrayList(listA);

System.out.println("List A (after) : " + listA);

System.out.println("List B (after) : " + listB);

System.out.println("Sub-List (after) : " + listSub);

System.out.println();

System.out.println("Are List's A and B equal? " + listA.equals(listB));





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Shuffle the elements around in some Random order for List A. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



System.out.println("List A (before) : " + listA);

System.out.println("List B (before) : " + listB);

System.out.println("Sub-List (before) : " + listSub);

System.out.println();

System.out.println("Are List's A and B equal? " + listA.equals(listB));

System.out.println();

Collections.shuffle(listA, new Random());

System.out.println("List A (after) : " + listA);

System.out.println("List B (after) : " + listB);

System.out.println("Sub-List (after) : " + listSub);

System.out.println();

System.out.println("Are List's A and B equal? " + listA.equals(listB));





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Convert a List to an Array. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



Object[] objArray = listA.toArray();

for (int j=0; j < objArray.length; j++) {

System.out.println("Array Element [" + j + "] = " + objArray[j]);

}





System.out.println();

System.out.println("+---------------------------------------------------------------------+");

System.out.println("| Remove (clear) Elements from List A. |");

System.out.println("+---------------------------------------------------------------------+");

System.out.println();



System.out.println("List A (before) : " + listA);

System.out.println("List B (before) : " + listB);

System.out.println();

listA.clear();

System.out.println("List A (after) : " + listA);

System.out.println("List B (after) : " + listB);

System.out.println();



}





/**

* Sole entry point to the class and application.

* @param args Array of String arguments.

*/

public static void main(String[] args) {

ArrayListExample listExample = new ArrayListExample();

listExample.doArrayListExample();

}



}
2016-11-23 20:43:30 UTC
hi, right it particularly is what i arise with, please ask the rest in case you like greater rationalization, or fee my answer if it helped. a million - type A without variations: public type A { ArrayList c; public A() { c = new ArrayList(); } public void upload(C neww) { c.upload(neww); } public C get(int i) { C get = c.get(i); return get; } } 2 - type B - 2 issues replaced: a million - in the constructor you ought to initialize (inner maximum A myC) its not initialized, and its consistently null. 2 - in the tactic (addToArrayInA) you're calling (A.upload(newC);) the place A is the class call, and can be the example of this manner. so right it particularly is the class: public type B { inner maximum A myC; public B() { myC = new A(); } public void addToArrayInA(int one, String 2) { C newC = new C(one, 2); //A.upload(newC); myC.upload(newC); } public C getC() { int i = 0; C cc = myC.get(i); return cc; } } 3 - type C: a toString() approach is extra to this manner, merely to make certain that we are doing the splendid ingredient: public type C { inner maximum int o; inner maximum String t; public C(int one, String 2) { o = one; t = 2; } @Override public String toString() { return "C [o=" + o + ", t=" + t + "]"; } } 4 - A finding out type that could help you: public type substantial { public static void substantial(String[] args) { B b = new B(); b.addToArrayInA(a million, "String"); C c = b.getC(); equipment.out.println(c.toString()); } } in this manner: a million - you create an occasion of sophistication b so which you will call the tactic (addToArrayInA) which provides a clean c to the checklist in A. 2 - Create a clean occasion of C, which holds the linked fee of the C merchandise in the checklist in A (little perplexing yet it particularly is it). 3 - Print the linked fee of C, using the quicker overridden toString() approach. desire this helps.
garry s
2007-05-15 16:11:35 UTC
This looks like a great homework assignment. Enjoy figuring it out...
Neilius
2007-05-15 16:39:48 UTC
Why yes, I'd love to do your homework for you!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...