/** * Test of ArrayLists basic instructions * * @ge * @4/16/18 */ import java.util.ArrayList; public class ArrayListTest { public static void main(String [] args) { ArrayList myList = new ArrayList(); //ArrayListmyList = new ArrayList (); // more efficient - eliminates warning Integer x = new Integer(45); // creates Integer 45 myList.add(x); // adds Integer 45 to myList myList.remove(x); // removes the value 45 from myList x = new Integer(100); // creates Integer 100 myList.add(0,x); // adds Integer 100 to the beginning of myList System.out.println(myList.get(0)); // print the first element System.out.println(myList.size()); // print the number of elements // One step further myList.add(5); myList.add(-27); myList.add(55); myList.add(0,3); } }
Classwork:
Let’s use the following documentation Java™ Platform, Standard Edition 8
API Specification to create our own resources:
From the documentation copy and paste the following methods’ description:
* 1
* boolean add(E e)
*
* 2
* void add(int index, E element)
* Having two methods with the same name but different argument list
* is called OVERLOADING
*
* 3
* E get(int index)
*
* 4
* E set(int index, E element)
*
* 5
* int indexOf(Object o)
*
* 6
* boolean isEmpty()
* Returns true if this list contains no elements.
*
* 7
* E remove(int index)
*
* 8
* boolean remove(Object o)
*
* 9
* int size()
Assignment:
ArrayListBasics_YI.java
Write a program that does the following:
Choose a meaningful and complete message for each item in the list.
- It creates an ArrayList, anAList of 100 random integers between any values from 1 to 1000 and find the two consecutive integers with the smallest difference between the two of them.
-
It finds and prints with a message the largest value in anAList.
-
It finds and prints with a message the average value in anAList.
-
It prints all the numbers from anAList separated with a space and in one line.
-
It prints the numbers from anAlist separated by 3 spaces in 10 columns by 10 rows format. Use printf to achieve this format. Do not use tab: “\t”.
-
Prompt the user for an integer and replace the 10th Integer with this new one. Display a message to acknowledge wether it was found.
-
Prompt the user for a number and search for that number. Display the number and the index where it was found.
-
Remove 10th Integer and print a message with the number.
-
Print the number of elements the ArrayList anAlist contains.
Note: Use printf to keep all your numbers tab properly.
/**** Make sure your documentation includes the output ****/