0% found this document useful (0 votes)
111 views10 pages

Java List Operations for FYMCA Lab

The document contains 6 programming assignments related to Java collections: 1. A program to demonstrate capabilities of the Collection interface using ArrayLists of colors. 2. A program to perform add, update, remove, and check operations on an ArrayList of company names. 3. A program to add numbers 1-20 to an ArrayList and print subsets using foreach loops. 4. A program to sort elements in an ArrayList. 5. A program to reverse elements in an ArrayList. 6. A program to perform add, remove, insert, replace, check and size operations on a LinkedList.

Uploaded by

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

Java List Operations for FYMCA Lab

The document contains 6 programming assignments related to Java collections: 1. A program to demonstrate capabilities of the Collection interface using ArrayLists of colors. 2. A program to perform add, update, remove, and check operations on an ArrayList of company names. 3. A program to add numbers 1-20 to an ArrayList and print subsets using foreach loops. 4. A program to sort elements in an ArrayList. 5. A program to reverse elements in an ArrayList. 6. A program to perform add, remove, insert, replace, check and size operations on a LinkedList.

Uploaded by

Jidnesh madhavi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SIES College of Management Studies FYMCA (Revised), Sem I,

Roll No: 25

Assignment 1

1) Write a Java Program to demonstrate various capabilities of interface ‘Collection’.


The program uses two color Arrays naming
a) colorsArray – contains elements as Violet, Indigo, Blue, Green, Yellow, Orange, Red
b) removeColorsArray – contain elements as Blue, Green, Orange
c) First make use of List Interface and create two ArrayLists naming colorList and
removeColorList.
d) Populate both the above ArrayList objects using the two mentioned Arrays.
e) Create a method naming ‘removeColors’ passing two Collection objects c1 and

c2 which accepts both the ArrayLists.


f) Make use of Iterator within the above ‘removeColors’ method to traverse

through the Collections and remove the appropriate color matched in the
another collection.

import [Link].*;
class Colors{
public static void main(String args[])
{

String colorsArray[]= new String[]


{"Violet","Indigo","Blue","Green","Yellow","Orange","Red"};

String removecolorsArray[]=new String[]{"Blue","Green","Orange"};


ArrayList<String>colorsList = new ArrayList<String>();
ArrayList<String>removecolorsList = new ArrayList<String>();
for(int i=0;i<[Link];i++)
{
[Link](colorsArray[i]);
}
for(int i=0;i<[Link];i++)
{
[Link](removecolorsArray[i]);
}

removeColors(colorsList,removecolorsList);
}

static void removeColors(ArrayList color1,ArrayList color2)


{
Iterator<String>iterat=[Link]();
while([Link]())
{
if([Link]([Link]()))
Subject: MCAL12 Advanced Java Lab Academic Year First Half
2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

{
[Link]();
}

}
[Link](color1);
}
}

2) Write a Java Program to create list and apply following operations:


a) add and display the following elements in the ArrayList object: Google, Apple, Amazon,
Facebook, Twitter, Oracle.
b) Write a Java program to update 3rd element in the above list by “Microsoft”.
c) Write a Java program to remove the last element from the list of objects
d) Write a Java program to check whether an element is available in the list

import [Link];

class Collection {

public static void main(String[] args) {


ArrayList<String> MNC = new ArrayList<String>();

[Link]("Google");

[Link]("Apple");

[Link]("Amazon");

[Link]("Facebook");

[Link]("Twitter");

[Link]("Oracle");

[Link]("Array List Objects are: " +MNC);


[Link]();
Subject: MCAL12 Advanced Java Lab Academic Year First Half
2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

[Link](2,"Microsoft");

[Link]("\n Updated Array List Objects are: " +MNC);


[Link]();

[Link]([Link]()-1);

[Link]("\n Updated Array List Objects are: " +MNC);


[Link]();

[Link]("\n Checking if Google is Present in List");

if([Link]("Google"))

[Link]("\n Element is Present");

else

[Link]("\n Element is Missing");

3) Write a java program to add 1 to 20 numbers to ArrayList object. Print the following
outputs using foreach loop.
a) The 10 elements from 1 to 20.
b) Only even numbers
c) Only numbers which are perfect squares

import [Link];
import [Link];
import [Link];

public class Java1 {


public static void main(String[] args) {
Subject: MCAL12 Advanced Java Lab Academic Year First Half
2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

ArrayList<Integer> list = new ArrayList<Integer>();

for(int i=1;i<=20;i++){
[Link](i);
}

[Link]("The 10 elements from 1 to 20 are");

for(int n : list){
if(n<=10){
[Link](n);
}
}
[Link]();
[Link]("Only even numbers are");

for(int num : list){


if(num%2==0){
[Link](num);
}
}

[Link]();
[Link]("Only numbers which are perfect squares are");

for(int sq : list){
if(([Link](sq) - [Link]([Link](sq)) == 0)){
[Link](sq);
}
}

}
}

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

4) Write a java program to sort the elements in an ArrayList.

import [Link];

import [Link];

public class Java1_4 {

public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<Integer>();


Subject: MCAL12 Advanced Java Lab Academic Year First Half
2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

[Link](5);

[Link](4);

[Link](9);

[Link](1);

[Link](0,10);

[Link]("ArrayList before sorting is");

[Link](list);

[Link]();

[Link]("Sorted ArrayList is");

[Link](list);

[Link](list);

5) Write a java program to reverse the elements in an ArrayList.

package Java1;

import [Link];

import [Link];

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

public class Java14 {

public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<Integer>();

[Link](5);

[Link](4);

[Link](9);

[Link](1);

[Link](0,10);

[Link]("ArrayList is");

[Link](list);

[Link]();

[Link]("Reverse of ArrayList is");

[Link](list);

[Link](list);

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

6) Write a java program to perform the following operations using the LinkedList class:
a. Add elements A to G. Display the contents of LinkedList.
b. Remove element “B”. Remove element at index 3. Remove the first
element. Remove the last element. Display the contents of LinkedList.
c. Insert element “X” as the first element. Insert element “Z” as the last
element.
d. Find the element “E”. If it exists, display, “List contains the element 'E':
else display “List does not contain the element 'E'”.
e. Display the size of LinkedList.

f. Replace element 5 with “Y” and display the contents

import [Link].*;

public class Lists{

public static void main(String args[])

LinkedList<String> list=new LinkedList<String>();

[Link]("A");

[Link]("B");

[Link]("C");

[Link]("D");

[Link]("E");

[Link]("F");

[Link]("G");

[Link](list);

[Link]("B");

[Link](list);

[Link](3);

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

[Link](list);

[Link]();

[Link]();

[Link](list);

[Link](0,"X");

[Link](4,"Z");

[Link](list);

if([Link]("E"))

[Link]("List contains the element 'E':");

else

[Link]("List does notcontains the element 'E':");

[Link]("LinkedList:" + list);

[Link]("The size of the linked list is: " + [Link]());

[Link](4,"Y");

[Link](list);

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24

Common questions

Powered by AI

Removing the last element from an ArrayList involves calling the 'remove()' method with the last index, calculated using 'size() - 1'. This operation decreases the size of the ArrayList by one and requires shifting any elements, which is not needed for the last element. Consequences include potential unchanged capacity of the list, though the logical size has decreased, meaning less memory is actively used until elements are added again .

An ArrayList in Java can be sorted using the 'Collections.sort()' method. This sorts the elements of the ArrayList in their natural order. Sorting is essential in scenarios where order matters, such as when implementing a leaderboard where scores need to be displayed from highest to lowest .

Choosing between ArrayList and LinkedList depends on use case: ArrayList is preferable for applications with frequent access operations because they support fast random access due to their underlying array implementation. On the other hand, LinkedList is more efficient for applications that involve frequent insertions and deletions, especially at the beginning or end of the list, due to its node structure which allows constant-time additions/removals compared to potential array resizing in ArrayLists .

The 'removeColors' method in Java uses the Iterator interface to traverse through the first collection ('colorList'). It checks each element to see if it is present in the second collection ('removeColorList'). If an element is found in both collections, it is removed from the first collection using the iterator's 'remove' method. This allows for a safe removal of elements while iterating, which is crucial to avoid ConcurrentModificationException .

Using 'Collections.reverse()' on an ArrayList reverses the order of elements in place without creating a new list. This in-place modification is efficient for memory usage but requires careful consideration of element ordering before and after the reversal. Effectively, it can be used when a reverse chronological order or a simple reversal of order is needed, like toggling between ascending and descending views .

To identify perfect squares in an ArrayList of integers, iterate through the list, and for each number, calculate its square root using 'Math.sqrt()'. If the square root is an integer (i.e., the square root value equals its floored value), then the number is a perfect square. This approach ensures that only numerically perfect squares like 1, 4, 9, 16, etc., are identified .

Removing an element from a LinkedList affects its structure by requiring traversal to the node and adjusting the links of adjacent nodes. Operations like removing the first or last element are efficient as they directly modify the head or tail reference, whereas removing from the middle involves more traversal. Insertion is similar; adding an element at the beginning or end is quick, but insertion elsewhere requires traversal. This linked nature makes LinkedList better for frequent insertions and deletions compared to ArrayList, but slower for index-based access .

To update elements in an ArrayList, you use the 'set' method which takes an index and the new value to be set at that index. For example, updating the third element involves calling 'set(2, "NewValue")'. If you attempt to update an index that does not exist, Java will throw an IndexOutOfBoundsException, because the ArrayList does not automatically extend its size or handle non-existent indices .

The 'set' method in LinkedList replaces an element at a specific index with a new value. This entails finding the node at the specified position and updating its value directly without affecting the surrounding nodes or structure. This operation is straightforward but involves O(n) complexity due to the need to traverse nodes sequentially until the index is reached, making it less efficient for large lists when accessing elements versus random access in an ArrayList .

The 'contains()' method is used to check for an element's existence in both ArrayList and LinkedList. This operation is linear with O(n) efficiency because it potentially requires checking each element until finding a match or reaching the end of the list. This linear search may be optimized in specific scenarios based on the list's ordering or other data structure specifics, but remains efficient enough for moderately sized collections .

SIES College of Management Studies
FYMCA (Revised), Sem I, 
Roll No: 25
Assignment 1
 
1) Write a Java Program to demonstrate
SIES College of Management Studies
FYMCA (Revised), Sem I, 
Roll No: 25
                   {
                       iterat.re
SIES College of Management Studies
FYMCA (Revised), Sem I, 
Roll No: 25
MNC.set(2,"Microsoft");
System.out.println("
 Update
SIES College of Management Studies
FYMCA (Revised), Sem I, 
Roll No: 25
        ArrayList<Integer> list = new ArrayList<Integ
SIES College of Management Studies
FYMCA (Revised), Sem I, 
Roll No: 25
 
4) Write a java program to sort the elements in an
SIES College of Management Studies
FYMCA (Revised), Sem I, 
Roll No: 25
        list.add(5);
        list.add(4);
        lis
SIES College of Management Studies
FYMCA (Revised), Sem I, 
Roll No: 25
public class Java14 {
    public static void main(Str
SIES College of Management Studies
FYMCA (Revised), Sem I, 
Roll No: 25
6) Write a java program to perform the following oper
SIES College of Management Studies
FYMCA (Revised), Sem I, 
Roll No: 25
System.out.println(list);   
list.removeFirst(); 
lis
SIES College of Management Studies
FYMCA (Revised), Sem I, 
Roll No: 25
Subject: MCAL12 Advanced Java Lab

You might also like