In: Computer Science
Determine the Output:
Package questions;
import java.util.*;
public class Quiz1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("*** 1 ***");
ArrayList<Integer>list1=new ArrayList<Integer>();
for(int i=0;i<30;i++)
{
list1.add(new Integer(i));
}
System.out.print("[");
for(int i=0;i<list1.size();i++)
{
System.out.print(list1.get(i)+" ");
}
System.out.println("]");
System.out.println("*** 2 ***");
ArrayList<Integer>list2=new ArrayList<Integer>();
for(int i=30;i<60;i++)
{
list2.add(i); //Auto Boxing an Integer not need to use new Integer
}
System.out.println(list2); //toString for an ArrayList --created by the Java Programmers
System.out.println("*** 3 ***");
ArrayList<Integer>list3=new ArrayList<Integer>();
list3.add(list1.remove(22)); //when an ArrayList removes an object it returns the object
list3.add(list1.remove(0));
list3.add(list1.remove(13));
list3.add(list1.remove(7));
list3.add(list1.remove(7));
list3.add(list1.remove(13)); //remember as an ArrayList removes the size is reduced
list3.add(list1.remove(11));
list3.add(list1.remove(list1.size()-1));
System.out.println(list3);
System.out.println("*** 4 ***");
System.out.println(list1);
System.out.println("*** 5 ***");
Collections.sort(list3);
System.out.println(list3);
System.out.println("*** 6 ***");
System.out.print("{ ");
for(int item:list3) //This is called unwrapping an Integer into an int
{
if(item<10)
System.out.print("0");
System.out.print(item+" ");
}
System.out.println("}");
}//end main
}//end class
/*
OUTPUT
*** 1 ***
*** 2 ***
*** 3 ***
*** 4 ***
*** 5 ***
*** 6 ***
*/
Package questions;
import java.util.*;
public class Quiz1{
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("*** 1 ***");
//to create an ArrayList that will hold the integer data
ArrayList<Integer>list1=new ArrayList<Integer>();
for(int i=0;i<30;i++)
{
list1.add(new Integer(i));//Here we are adding data from 0 to 29 in list1
}
System.out.print("[");
//loop over the list1
for(int i=0;i<list1.size();i++)
{
System.out.print(list1.get(i)+" ");//Here we are printing the data of list1
}
System.out.println("]");
System.out.println("*** 2 ***");
ArrayList<Integer>list2=new ArrayList<Integer>();
for(int i=30;i<60;i++)
{
//Here we are adding data from 30 to 59 in list2
list2.add(i); //Auto Boxing an Integer not need to use new Integer
}
//here we are printing the data of list2,internally it will call toString() method to print the output in [data1,data2,data3....] format
System.out.println(list2); //toString for an ArrayList --created by the Java Programmers
System.out.println("*** 3 ***");
ArrayList<Integer>list3=new ArrayList<Integer>();
//here when we remove data at given index of list1,so after removing data it will shift the indexes to the left
list3.add(list1.remove(22)); //when an ArrayList removes an object it returns the object
list3.add(list1.remove(0));
list3.add(list1.remove(13));
list3.add(list1.remove(7));
list3.add(list1.remove(7));
list3.add(list1.remove(13)); //remember as an ArrayList removes the size is reduced
list3.add(list1.remove(11));
list3.add(list1.remove(list1.size()-1));
//here we are printing list3 data
System.out.println(list3);
System.out.println("*** 4 ***");
//here we are printing the list1 after performing various removing operations
System.out.println(list1);
System.out.println("*** 5 ***");
//here we are sorting the data of list3 using Collections.sort() method
Collections.sort(list3);
//here we are printing list3 data
System.out.println(list3);
System.out.println("*** 6 ***");
System.out.print("{ ");
//here we are traversing each data of list3 using for-each loop
for(int item:list3) //This is called unwrapping an Integer into an int
{
if(item<10)
System.out.print("0");
System.out.print(item+" ");
}
System.out.println("}");
}//end main
}//end class
The Output of this code along with step by step explainations:
Output:
*** 1 ***
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ]
*** 2 ***
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
*** 3 ***
[22, 0, 14, 8, 9, 17, 15, 29]
*** 4 ***
[1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28]
*** 5 ***
[0, 8, 9, 14, 15, 17, 22, 29]
*** 6 ***
{ 00 08 09 14 15 17 22 29 }
Step by Step Explainations:
*** 1 ***
Here we are just printing the data of list1 by using a for loop,here we have formatted output as "[" and then print the data seperated by space and at the end we have add "]"
So the output will be :
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ]
*** 2 ***
Here we are just printing the data of list2 without using a for loop, here we just write System.out.println(list2), so internally it will call toString() method and the output will be printed as : [30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
*** 3 ***
This is a bit tricky part here we are removing various data from list1 and adding appropriate data in list3 as,
list3.add(list1.remove(22));
list3.add(list1.remove(0));
list3.add(list1.remove(13));
list3.add(list1.remove(7));
list3.add(list1.remove(7));
list3.add(list1.remove(13));
list3.add(list1.remove(11));
list3.add(list1.remove(list1.size()-1));
Here the output will be
[22, 0, 14, 8, 9, 17, 15, 29]
Note: Whenever we remove data from a particular valid index in ArrayList then the indexes will be shifted in left, for example when we remove 22 from list1, first it will remove 22 from list1 and then shift the indexes to the left by 1 and finally it will return the data that has been removed that is 22 and we have added 22 in list3.
In this way,the list3 [22, 0, 14, 8, 9, 17, 15, 29] is formed.
*** 4 ***
Here we have printed the updated list1(after performing various removal operations on it) as:
[1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28]
Note:we can clearly see that all the data which is present in list3 that is [22, 0, 14, 8, 9, 17, 15, 29], will not be present in list1 that is [1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28] as we have removed those data from list1
*** 5 ***
Here simply we have sorted the list3 [22, 0, 14, 8, 9, 17, 15, 29] by applying Collections.sort() on list3 and printed as:
[0, 8, 9, 14, 15, 17, 22, 29]
*** 6 ***
Here we have printing the data of list3 (after sorting it) in some condition that if the data is less then 10 then we just add a 0 in beginning and if it is greater then 10 we simply print it as:
{ 00 08 09 14 15 17 22 29 }
So in this way all the output of given program will be printed,If you are having any doubts regarding any thing in code or anything else that is not being understood,you please feel free to ask me in comment section,I will be more then happy to guide.
Screeshot of code: