0% found this document useful (0 votes)
18 views

Lab CSC248

The document provides examples of Java programs that perform operations on ArrayList objects, including adding, inserting, removing, and retrieving elements. It demonstrates how to declare and initialize ArrayLists, insert values at different index positions, calculate sums of elements, check for contained values, and remove elements based on conditions. The examples display the ArrayLists at different steps to show the effects of the operations.

Uploaded by

Hanis Riza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab CSC248

The document provides examples of Java programs that perform operations on ArrayList objects, including adding, inserting, removing, and retrieving elements. It demonstrates how to declare and initialize ArrayLists, insert values at different index positions, calculate sums of elements, check for contained values, and remove elements based on conditions. The examples display the ArrayLists at different steps to show the effects of the operations.

Uploaded by

Hanis Riza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSC248

1. Compile and run the program in below. Determine the output program
Answer:

2. Write a Java program that perform the following tasks:


i. Declare a sequential list called numberList with 10 elements.
ii. Insert 3 integers which are 8, 10, and 55. Display the list.
iii. Insert another 3 integers at the back which are 100, 157, 200. Display the list.
iv. Insert an integer 20 at index position 2.
v. Remove an element from the front once. Display the list.
vi. Remove all even integers. Display the list.

Answer:
import java.util.*;
import java.util.ArrayList;

public class Q2 {
public static void main (String[]args){

//1. Declare s sequential list called numberList with


10 elements.
ArrayList <Integer> numberList = new ArrayList (10);

//2. Insert 3 integers which are 8, 10, and 55. Display


the list.
numberList.add(8);
numberList.add(10);
numberList.add(55);

for(int i = 0 ; i<numberList.size(); i++){


System.out.println(numberList.get(i) + " ");
}

//3. Insert another 3 integers at the back which are


100, 157, 200. Display the list.
numberList.add(100);
numberList.add(157);
numberList.add(200);
System.out.println("\nCurrent list:");
for(int i = 0 ; i<numberList.size(); i++)
{
System.out.println(numberList.get(i) + " ");}

//4. Insert an integer 20 at index position 2


numberList.set(1,20);
{System.out.println("\nCurrent list:");}
for(int i = 0 ; i<numberList.size(); i++)
{

System.out.println(numberList.get(i) + " ");}

//5. Remove an element from the front once. Display the


list
numberList.remove(0);
{System.out.println("\nCurrent list:");}

for(int i = 0 ; i<numberList.size(); i++){

System.out.println(numberList.get(i) + " ");


}

//6. Remove all even integers. Display the list

for (int i = 0; i<numberList.size(); i++){


if(numberList.get(i) % 2 == 0){
numberList.remove(i);
}
}
System.out.println("\nCurrent list:");
for (int i = 0; i<numberList.size(); i++){
System.out.print(numberList.get(i) + " "); }
}
}
3. Fill the blank for the following program to complete the code program. Compile and
run. Then, display the output.

Answer:

import java.util.*;
public class Lab2_Q3{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);

//1. Declare a sequential list of Double class type named


numAL.
ArrayList <Double> numAL = new ArrayList <Double> ();

numAL.add(10.5); numAL.add(20.3);
numAL.add(15.5);
System.out.println("Contents of numAL: " + numAL);
//Asks user to enter another 3 floating point numbers.
//Display the list.
System.out.println("Please enter another 3 floating
points numbers:");
double num ;
for(int i=0; i<3; i++)
{ num= scan.nextDouble();
numAL.add(num); //2. add into list
}
System.out.println("Contents of numAL: " + numAL);
//3. Calculate and display the summation of elements in
an index position 0, 2, 4.
double sum = numAL.get(0) + numAL.get(2) + numAL.get(4);
//retrieve 3 elements

//4. Display sum


System.out.println("Total of the elements in an index
position 0, 2, 4 is " + sum);

//5. Check whether or not the list contains value 15.5


and display appropriate message.
double key = 15.5;
if(numAL.indexOf(15.5) != -1)

System.out.println("Value 15.5 is in the list numAL ");


//6. remove the elements which value more than 20.0.
for(int i=0; i<numAL.size(); i++)
{ if(numAL.get(i)>20.0)
numAL.remove(i); //call remove method
}
//7. Display the content of list
System.out.println("\nCurrent list:");
for (int i = 0; i<numAL.size(); i++){
System.out.print(numAL.get(i) + " "); }
}//end main
}//end class

Output:

You might also like