You are on page 1of 2

Laboratory Activity 2

A. Laboratory Exercise. Write a Java program given the following specifications:


Create an arraylist, add some colours (string) and print out the collection.
Insert an element into the arraylist at the first position.
Update specific array element by given element.
Search an element in an arraylist.
Join two arraylists.
Code:
package newActivity;
import java.util.ArrayList;
public class LabActivity {
public static void main(String[] args) {
ArrayList<String> Arrlist_Strings = new ArrayList<String>();
ArrayList<String> Arrlist_Strings2 = new ArrayList<String>();
System.out.println("1. Enter the Collection of colours: ");
Arrlist_Strings.add("White");
Arrlist_Strings.add("Blue");
Arrlist_Strings.add("Yellow");
Arrlist_Strings.add("Green");
Arrlist_Strings.add("Red");
System.out.println(Arrlist_Strings);
System.out.println("\n2. Insert element into the array list: Maroon");
Arrlist_Strings.add(0, "Maroon");
System.out.println("Array List After Inserting in the 1st index: "+Arrlist_Strings);
Arrlist_Strings.set(4, "Black");
System.out.println("\n3. Update specific array element: Black");
System.out.println("Array List After Update: "+Arrlist_Strings);
System.out.println("\n4. Search an element in an arraylist: Yellow");
int index = Arrlist_Strings.indexOf("Yellow");
if(index == -1){
System.out.println("The element Yellow is not in the arraylist ");
}else
System.out.println("The element Yellow is in the arraylist at index "+index);
Arrlist_Strings2.add("Violet");
Arrlist_Strings2.add("Browm");
Arrlist_Strings2.add("Grey");
Arrlist_Strings2.add("Orange");
Arrlist_Strings2.add("Indigo");
Arrlist_Strings.addAll(Arrlist_Strings2);
System.out.println("\n5.Join two arraylist:\n" +Arrlist_Strings);}}
B. Write a program that prompts the user for an integer, then asks the user to enter that many
values. Store these values in an array and print the array. Then reverse the array elements so that the
first element becomes the last element, the second element becomes the second to last element,
and so on, with the old last element now first. Do not just reverse the order in which they are printed;
actually change the way they are stored in the array. Do not create a second array; just rearrange the
elements within the array you have. (Hint: Swap elements that need to change places.) When the
elements have been reversed, print the array again.

Code:
package newActivity;
import java.util.ArrayList;
import java.util.Scanner;
public class LabActivity2 {
public static void main(String[] args) {
int a;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
a = scan.nextInt();
System.out.println("\nEnter the elements of the Array: ");
int[] array = new int[a];
for(int c = 0; c<a; c++){
array[c] = scan.nextInt();
}
System.out.println("\nArray elements are: ");
for(int c = 0; c<a; c++){
System.out.println(array[c]);
}
System.out.println("\nArray in Reverse order: ");
for(int c = (a-1); c>0;c--){
System.out.println(array[c] + " ");
}
}
}

You might also like