How can we write our own arraylist without using collections?

Showing Answers 1 - 27 of 27 Answers

SJ

  • Dec 18th, 2005
 

Using Linked List and Iterator?

  Was this answer useful?  Yes

tania

  • Dec 21st, 2005
 

List a=new ArrayList();

  Was this answer useful?  Yes

AKENDAR RANJIT SINGH

  • Dec 26th, 2005
 

You have to use array of Object to store the element and then provide the functionality of adding,seraching and removing the element.This is the way you can create your own arrayList without using Collections.

  Was this answer useful?  Yes

Vivek Menon

  • Jan 12th, 2006
 

LinkedList and Set are subclass of Collection and hence are Collections.Therefore using LinkedList is not a solution.Instead, u will have to create an array of Objects

  Was this answer useful?  Yes

sandhya

  • Apr 1st, 2007
 


    hi all !  
i suppose the question was not to use the collections than i hope even linked list comes into the collections package.

  Was this answer useful?  Yes

Ab mj

  • Nov 2nd, 2014
 

Without using collections and arraylist ? Then I guess we have to create our own collection and arraylist classes with methods similar to their implementation which uses array objects. Why again create same class? Better we can use arraylist itself .

  Was this answer useful?  Yes

chaitanya

  • Dec 19th, 2014
 

But if we r using arry instead of Arrylist...Array is immutable we canot add and delete the array elements

  Was this answer useful?  Yes

kalyankarri

  • Dec 26th, 2014
 

Code
  1. ArrayList al=new ArrayList();

  2. al.addelement(10);

  3. al.addelement(20);................

  4. system.out.printn("elements ara",+al);


  Was this answer useful?  Yes

amol thete

  • Jan 27th, 2015
 

use Iterator Design pattern

  Was this answer useful?  Yes

Yoyo

  • Mar 21st, 2015
 

@kalyankarri The questions says without an Arraylist

  Was this answer useful?  Yes

Amit

  • Jun 26th, 2015
 

Code
  1. package collections;

  2.  

  3. import java.util.Arrays;

  4.  

  5. public class MyArray {

  6.  

  7.         private Object[] myStore;

  8.         private int actSize = 0;

  9.        

  10.         public MyArray(){

  11.                 myStore = new Object[10];

  12.         }

  13.        

  14.         public Object get(int index){

  15.                 if(index < actSize){

  16.                         return myStore[index];

  17.                 }else{

  18.                         throw new ArrayIndexOutOfBoundsException();

  19.                 }

  20.         }

  21.         public void add(Object obj){

  22.                 if(myStore.length - actSize < 5){

  23.                         increaseSize();

  24.                 }else{

  25.                         myStore[actSize++] = obj;

  26.                 }

  27.                        

  28.         }

  29.        

  30.         private void increaseSize(){

  31.                 myStore = Arrays.copyOf(myStore, myStore.length*2);

  32.                 System.out.println("new length = "+ myStore.length);

  33.         }

  34.        

  35.         public int size(){

  36.                 return actSize;

  37.         }

  38.        

  39.         public static void main(String[] args) {

  40.                 // TODO Auto-generated method stub

  41.                

  42.                 MyArray my = new MyArray();

  43.                 my.add(new Integer(10));

  44.                 my.add(new Integer(20));

  45.                 my.add(new Integer(30));

  46.                 my.add(new Integer(40));

  47.                 my.add(new Integer(50));

  48.                

  49.                 for(int i=0;i< my.size();i++){

  50.                         System.out.println("Element = "+ my.get(i));

  51.                 }

  52.         }

  53.  

  54. }

  55.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions