In: Computer Science
Could I please get explainations and working out as to how to get to the answers.
Note that is where part of the answer goes.
1.
Complete the following function that returns true if ArrayLists
list1 and list2 are exactly the same in terms of contents
(same items in same order), false otherwise.
public static sameSame(ArrayList<Integer> list1,
ArrayList<Integer> list2) {
if(list1 == null || list2 == null)
return false;
if( != )
return false;
for(int i=0; i < list1.size(); i++) {
if( .equals( )==false) {
return ;
}
}
return ;
}
2.
Complete the missing statements so that the output of the
following code is:
[[10, 70, 20, 90], null, [50, 80]]
Do NOT include spaces in your answer!
ArrayList<Integer> b = new
ArrayList<Integer>(Arrays.asList(10,888,20,90));
ArrayList<Integer> a = null;
ArrayList<Integer> c = ; //make a reference copy
only!
b.set(1, 70);
b = new ArrayList<Integer>(Arrays.asList(50, 80));
ArrayList< > list = new ArrayList< >();
list.add( );
list.add( );
list.add( );
System.out.println(list);
1) //Here we need use boolean as return type, because we need to
return the value true or false, which states whether list1 and
list2 are same or different
public static boolean
sameSame(ArrayList<Integer> list1, ArrayList<Integer>
list2) {
if(list1 == null || list2 ==
null)
return
false;
//If the number of elements in the
lists are different, we can say that lists are not same. So, here
we are checking the size of the lists and if their sizes are not
equal, we are returning false.
if( list1.size() != list2.size()
)
return
false;
for(int i=0; i < list1.size();
i++) {
//In the
question it is mentioned that both the lists should contain same
items in same order. So, we are comparing the values in list1 with
the elements in list2 at same index(i). Here i moves from index 0
to the index before end of the list.
if(
(list1.get(i)).equals(list2.get(i) )==false) {
//If any of the values in the lists does not
match, we return false.
return false;
}
}
//The program execution reaches
this place, only if both the lists are not empty, and their sizes
are same and all their element values are same in same order. So,
here we return true.
return true;
}
2) ArrayList<Integer> b = new
ArrayList<Integer>(Arrays.asList(10,888,20,90));
ArrayList<Integer> a =
null;
//We are assigning array 'b' to
'c', because the value of array 'b' is changing to [50,80] later.
To preserve current value of array 'b', we are creating a reference
to array 'b' using 'c'. Here the value of array 'b' and 'c' is
(10,888,20,90).
ArrayList<Integer> c = b ;
//make a reference copy only!
//Here the value at index 1 of
array 'b', which is 888, is set to 70. So, now the value of array
'b' and 'c' is (10,70,20,90).
b.set(1, 70);
//Here value of array 'b' is
reassigned to (50,80). But value of array 'c' is (10,70,20,90) as
it is pointing to previous list of array 'b'.
b = new
ArrayList<Integer>(Arrays.asList(50, 80));
//Here we are adding the arrays
'a','b','c' to another list 'list'. As the datatype of arrays
'a','b','c' is ArrayList<Integer>, the ArrayList 'list'
holding type should be 'ArrayList<Integer>'
ArrayList<
ArrayList<Integer> > list = new ArrayList<
ArrayList<Integer> >();
//As per the output, [10, 70, 20,
90] should be displayed first. So, we are adding the array 'c'
which has these values.
list.add( c );
//As per the output, null should be
displayed next. So, we are adding the array 'a' which has the null
value.
list.add( a );
//As per the output, [50, 80]
should be displayed next. So, we are adding the array 'b' which has
these values.
list.add( b );
System.out.println(list);
Below is the complete program if you want to execute and check it:
ListsProgram.java:
import java.util.ArrayList;
import java.util.Arrays;
public class ListsProgram {
public static void main(String args[]) {
ArrayList<Integer> list1 =
new
ArrayList<Integer>(Arrays.asList(10,40,20,50,60,50));
ArrayList<Integer> list2 =
new
ArrayList<Integer>(Arrays.asList(10,40,20,50,60,50));
boolean result =
sameSame(list1,list2);
System.out.println("both are same:"
+ result);
printArrays();
}
public static boolean
sameSame(ArrayList<Integer> list1, ArrayList<Integer>
list2) {
if(list1 == null || list2 ==
null)
return
false;
if( list1.size() != list2.size()
)
return
false;
for(int i=0; i < list1.size();
i++) {
if(
(list1.get(i)).equals(list2.get(i) )==false) {
return false;
}
}
return true;
}
public static void printArrays()
{
ArrayList<Integer> b = new
ArrayList<Integer>(Arrays.asList(10,888,20,90));
ArrayList<Integer> a =
null;
ArrayList<Integer> c = b ;
//make a reference copy only!
b.set(1, 70);
b = new
ArrayList<Integer>(Arrays.asList(50, 80));
ArrayList<
ArrayList<Integer> > list = new ArrayList<
ArrayList<Integer> >();
list.add( c );
list.add( a );
list.add( b );
System.out.println(list);
}
}