Java1 25
Java1 25
Roll No: 25
Assignment 1
through the Collections and remove the appropriate color matched in the
another collection.
import java.util.*;
class Colors{
public static void main(String args[])
{
removeColors(colorsList,removecolorsList);
}
{
iterat.remove();
}
}
System.out.println(color1);
}
}
import java.util.ArrayList;
class Collection {
MNC.add("Google");
MNC.add("Apple");
MNC.add("Amazon");
MNC.add("Facebook");
MNC.add("Twitter");
MNC.add("Oracle");
MNC.set(2,"Microsoft");
MNC.remove(MNC.size()-1);
if(MNC.contains("Google"))
else
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 java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
for(int i=1;i<=20;i++){
list.add(i);
}
for(int n : list){
if(n<=10){
System.out.println(n);
}
}
System.out.println();
System.out.println("Only even numbers are");
System.out.println();
System.out.println("Only numbers which are perfect squares are");
for(int sq : list){
if((Math.sqrt(sq) - Math.floor(Math.sqrt(sq)) == 0)){
System.out.println(sq);
}
}
}
}
import java.util.ArrayList;
import java.util.Collections;
list.add(5);
list.add(4);
list.add(9);
list.add(1);
list.add(0,10);
System.out.println(list);
System.out.println();
Collections.sort(list);
System.out.println(list);
package Java1;
import java.util.ArrayList;
import java.util.Collections;
list.add(5);
list.add(4);
list.add(9);
list.add(1);
list.add(0,10);
System.out.println("ArrayList is");
System.out.println(list);
System.out.println();
Collections.reverse(list);
System.out.println(list);
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.
import java.util.*;
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.add("F");
list.add("G");
System.out.println(list);
list.remove("B");
System.out.println(list);
list.remove(3);
System.out.println(list);
list.removeFirst();
list.removeLast();
System.out.println(list);
list.add(0,"X");
list.add(4,"Z");
System.out.println(list);
if(list.contains("E"))
else
System.out.println("LinkedList:" + list);
list.set(4,"Y");
System.out.println(list);