Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Answer in terms of Java! In your daily life, you often use lists. How do you use

ID: 3793840 • Letter: A

Question

Answer in terms of Java!

In your daily life, you often use lists. How do you use a list? Think about building a list. How do you do it? Do you always start with the first item? How do you add new items? How do you remove items? How would you make a new list from two different lists?

There are two ways to implement a linked list. What are they and how do you implement them? Why would you choose one over the other?

Do you see any potential problems that a programmer might have in implementing or using linked list? If so, what are they and what would you do to alleviate them? Does it have to do with size? Does it have to do with access?

Explanation / Answer

List is interface which i implement by three classes like as ArrayList,LinkedList and Vector. We used list for adding and deleting element by index basis. It allow duplicates and maintained insertion sequence order. As example given below

package com.jbk;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class ArrayListTest {
   public static void main(String[] args) throws Exception {
      
      
       ArrayList al = new ArrayList();
       al.add(new Integer(56));
       al.add(new Integer(56));
       al.add(new Integer(78));
            
       // for loop
       for (int i = 0; i < al.size(); i++) {
           Object object = al.get(i);
           Integer ii = (Integer) object;
           int x = ii.intValue();
           System.out.println(x);
       }
       // Iterator
       System.out.println("-===== By iterator -===== ");
       Iterator itr = al.iterator();

       while (itr.hasNext()) {
           Object object = itr.next();
           Integer ii = (Integer) object;
           int x = ii.intValue();
           System.out.println(x);
       }

       System.out.println("-===== By List Iterator -===== ");

       ListIterator litr = al.listIterator();

       while (litr.hasNext()) {
           Object object = litr.next();
           Integer ii = (Integer) object;
           int x = ii.intValue();
           System.out.println(x);
       }

   }
}

Output:56
56
78
-===== By iterator -=====
56
56
78
-===== By List Iterator -=====
56
56
78

//Here we append two arraylist in to one arraylist
//by using generic Concept
package com.jbk;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class ArrayListTest {
   public static void main(String[] args) throws Exception {
      
       ArrayList al = new ArrayList();
       al.add(new Integer(56));
       al.add(new Integer(56));
       al.add(new Integer(78));

       ArrayList al1 = new ArrayList();
       al1.add(new Integer(567));
       al1.add(new Integer(24));
       al1.add(new Integer(51));
      
ArrayList<ArrayList> al2 =new ArrayList<ArrayList>();
       al2.add(al);
       al2.add(al1);
       System.out.println(al2);
      

       // Iterator
       System.out.println("-===== By iterator -===== ");
       Iterator itr = al2.iterator();

       while (itr.hasNext()) {
           Object array = itr.next();
          
           Iterator iitr = ((ArrayList) array).iterator();

           while (iitr.hasNext()) {
               Object object = iitr.next();
               Integer ii = (Integer) object;
               int x = ii.intValue();
               System.out.println(x);
           }
          
          
}

   }
}

Output:[[56, 56, 78], [567, 24, 51]]
-===== By iterator -=====
56
56
78
567
24
51

Here implementing LinkedList as below

package com.jbk;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class ArrayListTest {
   public static void main(String[] args) throws Exception {
       LinkedList al = new LinkedList();
       al.add(new Integer(56));
       al.add(new Integer(56));
       al.add(new Integer(78));
       System.out.println(al);

      
       //For LinkedList
       System.out.println("-===== By Iterator -===== ");

   Iterator litr = al.iterator();

       while (litr.hasNext()) {
           Object object = litr.next();
           Integer ii = (Integer) object;
           int x = ii.intValue();
           System.out.println(x);

   }
   }
}

Output as:[56, 56, 78]
-===== By Iterator -=====
56
56
78

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote