Question

In: Computer Science

Provide two page paper describing about Array list and operations & Iterators with very suitable examples.

Provide two page paper describing about Array list and operations & Iterators with very suitable examples.

Solutions

Expert Solution

ArrayList is based on Array data structure and implementes List interface. ArrayList is a resizable-array.

Array has fixed length. If the array is full you can't add more elements in it. To overcome this problem, arrayList is used. It can dynamically grow with no size limit. So it is called resizable array.

  • It has duplicate elements also.
  • It implements List interface and inherits AbstractList class
  • It maintain insertion order
  • It allows random access just like array

syntax of class ArrayList :
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

You can defined arrayList as :
ArrayList<Integer> arrList = new ArrayList<Integer>();
It will create a empty arrayList named arrList.

Basic operations on arrayList :

  • void add(E e) - It will apppend e at the end of the list.

For e.g.
   arrList.add(5)
   arrList.add(10)
It will add 5 and 10 in the arrList
Thus, when you print arrList it will give output as [5, 10]

  • E remove(int index) - It will remove the element present at that index.

For e.g.
   arrList.remove(1)
At index 1, we have 10. So it will be removed.
Thus, when you print arrList it will give output as [5]

  • boolean isEmpty() - If the list is empty, it will return true, false otherwise.

For e.g.
   arrList.isEmpty()
It will return false as we have one element in the arrList.

  • E get(int index) - It will return the element placed at given index

For e.g.
   arrList.get(0)
It will return 5, as at index 0, we have 5

  • int indexOf(Object o) - It will return the index of the given object and -1 if the object is not present in the given arrayList

For e.g.
   arrList.indexOf(5)
It will return 0, as at index 0, we have 5

  • void clear() - It will remove all the elements from given arrayList

For e.g.
   arrList.clear()
Now, arrList becomes null

  • E set(int index, E element) - It will add given element at given index

For e.g.
   arrList.set(0,10)
It will add 10 at 0th position.

  • int size() - It will return the number of elements present in the list

For e.g.
   arrList.size()
It will return 1 as we have only one element in arrList

Iterators :

To iterate a arrayList, we have iterator() method

Iterator iterator()

No any parameters are needed in this method. It returns an iterator over the elements presents in the list.

See given code snippet :

ArrayList<String>arrlist = new ArrayList<String>();  //It will create arrayList of string type
//adds an elements in the list
arrlist.add("abc");  
arrlist.add("def");  
arrlist.add("ghi");  
arrlist.add("jkl");  
 
System.out.println(arrlist);    // [abc, def, ghi, jkl]  

//To initialize iterator of string type on arrlist  
Iterator<String> iterator = arrlist.iterator();  
while (iterator.hasNext())  
{  
        String i = iterator.next();  
        System.out.println(i);    
}  

In each iteration, it will print one element of an arrlist

In above example,
hasNext() method is used to loop through whole arrlist

next() will return the next element

You can also use for loop to iterate an arrayList same as below

    for (int i : arrlist) {
      System.out.println(i);
    }

Related Solutions

2 page paper describing the following Provide a description of an organization you are familiar with...
2 page paper describing the following Provide a description of an organization you are familiar with (possibly a company you have worked for in the past or are currently working for) and describe briefly what services they contribute. Then, select three out of the six questions below and provide detailed answers, supporting those answers by referencing any sources used. Be sure to use examples from your research to strengthen your argument as needed ·What personal knowledge management tools does this...
One to two-page Paper describing the various forms of child abuse, how to recognize it, and...
One to two-page Paper describing the various forms of child abuse, how to recognize it, and reporting requirements.
Write a three-page paper summarizing each of the following areas about France and describing the influence...
Write a three-page paper summarizing each of the following areas about France and describing the influence of each on global business: Religion (reflect on how your personal beliefs and values will be affected by doing business in this country) Government Technology structures Keep track of your references, as you will need to compile these at the conclusion of your project.
Write a two (2) page paper describing the differences and interactions between strategic thinking and strategic...
Write a two (2) page paper describing the differences and interactions between strategic thinking and strategic management. Address why many leaders often fail to think strategically. Include experiences you have had with leaders for whom you have worked
Describe the three business activities of financing, investing and operating. ----------------------------------------------------------------------------------------------------------------------- Write a two-page paper describing...
Describe the three business activities of financing, investing and operating. ----------------------------------------------------------------------------------------------------------------------- Write a two-page paper describing the financing, investing, and operating activities of a business. You might find it easier to pick a business of your choice and write about that business specifically. Be sure to describe what each business activity represents. Use a separate heading for each of the 3 activities (financing, investing and operating). Give at least two examples of different items (i.e. financial transactions) that can be...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You...
(Paragraph Form). One to two-page Paper describing the various forms of child abuse, how to recognize...
(Paragraph Form). One to two-page Paper describing the various forms of child abuse, how to recognize it, and reporting requirements.
Write two-page paper including your list of sources about your favorite economic concept and its application...
Write two-page paper including your list of sources about your favorite economic concept and its application in everyday life
write a short one-half to one page response paper describing what you learned about computational science...
write a short one-half to one page response paper describing what you learned about computational science and anything interesting about it
detailed paper about Hospitality in project management , please provide me with a 1 page introduction...
detailed paper about Hospitality in project management , please provide me with a 1 page introduction including thesis statement 500 word , no copy and paste please hospitality in project management
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT