1. R6.31 Section 6.8.8 shows that you must be careful about updating the index v
ID: 3876248 • Letter: 1
Question
1. R6.31 Section 6.8.8 shows that you must be careful about updating the index value when you remove elements from an array list. Show how you can avoid this problem by traversing the array list backwards.
2. R6.33 How do you perform the following tasks with array lists in Java?
a. Test that two array lists contain the same elements in the same order.
b. Copy one array list to another.
c. Fill an array list with zeroes, overwriting all elements in it.
d. Remove all elements from an array list.
3. R6.34 True or false?
a. All elements of an array list are of the same type.
b. Array list index values must be integers.
c. Array lists cannot contain strings as elements.
d. Array lists can change their size, getting larger or smaller.
Explanation / Answer
Question 2
a. Two araylist created with same order
import java.util.*;
class Test{
public static void main(String args[])
{
ArrayList<Integer> num=new ArrayList<Integer>();//Creating arraylist
num.add(1);//Adding object in arraylist
num.add(2);
num.add(3);
num.add(4);
ArrayList<Integer> num2=new ArrayList<Integer>();//Creating arraylist
num2.add(11);//Adding object in arraylist
num2.add(12);
num2.add(13);
num2.add(14);
//Traversing list through Iterator
Iterator itr=num.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
//Traversing list through Iterator
Iterator itr2=num2.iterator();
while(itr2.hasNext())
{
System.out.println(itr2.next());
}
}
}
b.Copying one arraylist to anothere
import java.util.*;
class Test{
public static void main(String args[])
{
ArrayList<Integer> num2=new ArrayList<Integer>();//Creating arraylist
num2.add(11);//Adding object in arraylist
num2.add(12);
num2.add(13);
num2.add(14);
ArrayList<Integer> num3 = new ArrayList<Integer>() ;
for (int i = 0 ; i<num2.size();i++)
{
num3.add(num2.get(i)) ;
}
Iterator itr=num3.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
c.Replacing the elements of ArrayList with 0
import java.util.*;
class Test{
public static void main(String args[])
{
ArrayList<Integer> num2=new ArrayList<Integer>();//Creating arraylist
num2.add(11);//Adding object in arraylist
num2.add(12);
num2.add(13);
num2.add(14);
ArrayList<Integer> num3 = new ArrayList<Integer>() ;
for (int i = 0 ; i<num2.size();i++)
{
num3.add(num2.get(i)) ;
}
System.out.println("Before Replacing");
Iterator itr=num3.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
System.out.println("After Replacing");
for (int i = 0 ; i<num2.size();i++)
{
num3.add(0) ;
}
}
}
d.Removing the elements from the array list
import java.util.*;
class Test{
public static void main(String args[])
{
ArrayList<Integer> num2=new ArrayList<Integer>();//Creating arraylist
num2.add(11);//Adding object in arraylist
num2.add(12);
num2.add(13);
num2.add(14);
ArrayList<Integer> num3 = new ArrayList<Integer>() ;
for (int i = 0 ; i<num2.size();i++)
{
num3.add(num2.get(i)) ;
}
System.out.println("Before Removing");
Iterator itr=num3.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
num3.clear();
System.out.println("After Removing");
Iterator itr1=num3.iterator();
while(itr1.hasNext())
{
System.out.println(itr1.next());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.