JAVA Programmingwith ArrayList
1.Create twoArrayListsto store Stringsand Integer typeof data.
a. Add 10 data in each list using add() method.
b. Print the content in both ArrayListsusing forloop.
c. Add 5 new data in each list from index number 3.
d. Print the content in both ArrayListsusing for-each loop.
e. Clear both list using clear() and add 12new elementsagain.
f. Print the content of the new list using iterator.
g. Removeelement from index number 2 and remove any one element using remove(Object e) method.
h. Sort the list using Collections.sort() method.
i. Explain use of get() and set() method using any of the two ArrayList.
CODE :
import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;
public class ArrayListExample {
public static void main(String[] args) {
int n = 15;
ArrayList<Integer> array1
= new ArrayList<Integer>(n);
array1.add(11);
array1.add(12);
array1.add(13);
array1.add(14);
array1.add(15);
array1.add(16);
array1.add(17);
array1.add(18);
array1.add(19);
array1.add(20);
System.out.println("Array list 1 :- ");
for (int i = 0; i < array1.size(); i++) {
System.out.print(array1.get(i) + " ");
}
ArrayList<String> array2
= new ArrayList<String>(n);
array2.add("cow");
array2.add("dog");
array2.add("xyz");
array2.add("def");
array2.add("girl");
array2.add("boy");
array2.add("this");
array2.add("coffie");
array2.add("cat");
array2.add("maruti");
System.out.println("\n\nArray list 2 :- ");
for (int i = 0; i < array2.size(); i++) {
System.out.print(array2.get(i) + " ");
}
ArrayList<Integer> array3
= new ArrayList<Integer>(n);
array3.add(55);
array3.add(56);
array3.add(57);
array3.add(58);
array3.add(59);
array1.addAll(3,array3);
System.out.println("\n\nArray list 1 after add 5 new data from index 3 :- ");
for (Integer i : array1) {
System.out.print(i+ " ");
}
ArrayList<String> array4
= new ArrayList<String>(n);
array4.add("ABC");
array4.add("white");
array4.add("jun");
array4.add("march");
array4.add("warning");
array2.addAll(array4);
System.out.println("\n\nArray list 2 after add 5 new data from index 3 :- ");
for (String s:array2) {
System.out.print(s+ " ");
}
array1.clear();
System.out.print("\n\nThe array list 1 after using clear() method :-\n"+array1);
array1.add(61);
array1.add(62);
array1.add(63);
array1.add(64);
array1.add(65);
array1.add(66);
array1.add(67);
array1.add(68);
array1.add(69);
array1.add(70);
array1.add(71);
array1.add(72);
ListIterator<Integer> iterator1 = array1.listIterator();
System.out.println("\n\nAdd new 12 elements in array list 1 using iterator :-");
while (iterator1.hasNext()) {
System.out.print(iterator1.next()+" ");
}
array2.clear();
System.out.print("\n\nThe array list 2 after using clear() method :-\n"+array2);
array2.add("clap");
array2.add("UV");
array2.add("run");
array2.add("body");
array2.add("snap");
array2.add("os");
array2.add("car");
array2.add("ram");
array2.add("rohit");
array2.add("cm");
array2.add("veer");
array2.add("tarun");
ListIterator<String> iterator2 = array2.listIterator();
System.out.println("\n\nAdd new 12 elements in array list 2 using iterator :-");
while (iterator2.hasNext()) {
System.out.print(iterator2.next()+" ");
}
array1.remove(2);
System.out.println("\n\nArray list 1 after remove element from index number 2 :- \n"+array1);
array1.remove(new Integer(58));
System.out.println("\n\nArray list 1 after using remove(object e) :- \n"+array1);
array2.remove(2);
System.out.println("\n\nArray list 2 after remove element from index number 2 :- \n"+array1);
array2.remove("door");
System.out.println("\n\nArray list 2 after using remove(object e) :- \n"+array2);
Collections.sort(array1);
System.out.println("\nArray list 1 after using Collections.sort() :-\n" + array1);
Collections.sort(array2);
System.out.println("\nArray list 2 after using Collections.sort() :-\n" + array2);
int i1 = array1.set(3,98);
System.out.println("\nArray list 1 after using set() :-\n" + array1);
System.out.println("Replace element :-"+i1);
int i2 = array1.get(8);
System.out.println("\nUsing get() :-");
System.out.println("The element of index 8 :-"+i2);
String s1 = array2.set(5,"bird");
System.out.println("\nArray list 2 after using set() :-\n" + array2);
System.out.println("Replace element :-"+s1);
String s2 = array2.get(7);
System.out.println("\nUsing get() :-");
System.out.println("The element of index 7 :-"+s2);
}
}
OUTPUT:
2.Create a class for Student with below properties.
i. Name
ii. Enrolment Number
iii. Semester
iv. Result (CGPA)
b. Initialize all the variables of student class using parameterized constructor(pass values while creating objects).
c. Create an ArrayList to store the data of type Student. Add ten different student objects in the ArrayList.
d. Create iterator over the ArrayList and print all the details of each student by iterating over the list.
CODE :
import java.util.ArrayList;
import java.util.ListIterator;
class Student{
String name;
long enroll;
int sem;
float result;
Student(String name,long enroll,int sem,float result) {
this.name = name;
this.enroll = enroll;
this.sem = sem;
this.result = result;
}
public static void main (String[] args) {
ArrayList array = new ArrayList();
System.out.println("Name Enroll_number Semester Result(CGPA)\n");
Student s1 = new Student("XYZ1",180210107001l,3,8.1f);
array.add(s1.name);
array.add(s1.enroll);
array.add(s1.sem);
array.add(s1.result);
ListIterator iterator1 = array.listIterator();
while(iterator1.hasNext()){
System.out.print(iterator1.next()+" ");
}
array.clear();
System.out.println("\n");
Student s2 = new Student("XYZ2",180210107002l,3,6f);
array.add(s2.name);
array.add(s2.enroll);
array.add(s2.sem);
array.add(s2.result);
ListIterator iterator2 = array.listIterator();
while(iterator2.hasNext()){
System.out.print(iterator2.next()+" ");
}
array.clear();
System.out.println("\n");
Student s3 = new Student("XYZ3",180210107003l,3,5.9f);
array.add(s3.name);
array.add(s3.enroll);
array.add(s3.sem);
array.add(s3.result);
ListIterator iterator3 = array.listIterator();
while(iterator3.hasNext()){
System.out.print(iterator3.next()+" ");
}
array.clear();
System.out.println("\n");
Student s4 = new Student("XYZ4",180210107004l,3,9.0f);
array.add(s4.name);
array.add(s4.enroll);
array.add(s4.sem);
array.add(s4.result);
ListIterator iterator4 = array.listIterator();
while(iterator4.hasNext()){
System.out.print(iterator4.next()+" ");
}
array.clear();
System.out.println("\n");
Student s5 = new Student("XYZ5",180210107005l,3,7.6f);
array.add(s5.name);
array.add(s5.enroll);
array.add(s5.sem);
array.add(s5.result);
ListIterator iterator5 = array.listIterator();
while(iterator5.hasNext()){
System.out.print(iterator5.next()+" ");
}
array.clear();
System.out.println("\n");
Student s6 = new Student("XYZ6",180210107006l,3, 8.6f);
array.add(s6.name);
array.add(s6.enroll);
array.add(s6.sem);
array.add(s6.result);
ListIterator iterator6 = array.listIterator();
while(iterator6.hasNext()){
System.out.print(iterator6.next()+" ");
}
array.clear();
System.out.println("\n");
Student s7 = new Student("XYZ7",180210107007l,3,9.6f);
array.add(s7.name);
array.add(s7.enroll);
array.add(s7.sem);
array.add(s7.result);
ListIterator iterator7 = array.listIterator();
while(iterator7.hasNext()){
System.out.print(iterator7.next()+" ");
}
array.clear();
System.out.println("\n");
Student s8 = new Student("XYZ8",180210107008l,3,9.9f);
array.add(s8.name);
array.add(s8.enroll);
array.add(s8.sem);
array.add(s8.result);
ListIterator iterator8 = array.listIterator();
while(iterator8.hasNext()){
System.out.print(iterator8.next()+" ");
}
array.clear();
System.out.println("\n");
Student s9 = new Student("XYZ9",180210107009l,3,8.9f);
array.add(s9.name);
array.add(s9.enroll);
array.add(s9.sem);
array.add(s9.result);
ListIterator iterator9 = array.listIterator();
while(iterator9.hasNext()){
System.out.print(iterator9.next()+" ");
}
array.clear();
System.out.println("\n");
Student s10 = new Student("XYZ10",180210107010l,3,6.9f);
array.add(s10.name);
array.add(s10.enroll);
array.add(s10.sem);
array.add(s10.result);
ListIterator iterator10 = array.listIterator();
while(iterator10.hasNext()){
System.out.print(iterator10.next()+" ");
}
}
}
OUTPUT :
DOWNLOAD PDF HERE...... |
No comments:
Post a Comment