import java.util.Arrays;
                    import java.util.List;
                    import java.util.Random;

                    class DynamicArrayOfInts {

                        private int[] storage;
                        private int size;
                        private final  int INITIAL_CAPACITY = 8;
                        private final int GROW_FACTOR = 2;

                        public DynamicArrayOfInts() {
                          storage = new int[INITIAL_CAPACITY];
                          size = 0;
                        }

                        private void rangeCheck(int index){
                    if (index < 0 || index >= size)

                                      if(index < 0 || index >= size)
                          throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);

                        }

                        private void ensureCapacity(int size_wanted) {
                          int max_capacity = storage.length;
                          if (size_wanted > max_capacity) {
                            max_capacity = max_capacity * GROW_FACTOR +1;
                            storage = Arrays.copyOf(storage, max_capacity); // increases array size + copy contents
                          }
                        }

                        public int size() {
                          return size;

                        }

                         public boolean equals(Object aThat) { // aThat is a DynamicArrayOfInts object
                    if (aThat==this) {
                        return true;
                    } else {
                           return false;  // added so code would compile
                        }
                    }
                        public boolean equals(List<Integer> list)  { // list is a LinkedList, or ArrayList, etc

                           return false;  // added so code would compile
                        }

                        public int get(int position){
                    rangeCheck(position);
                    return storage [position];

                        }

                        public void set(int index, int value){
                            rangeCheck(index);

                                        storage[index] = value;


                        }

                        public void insertAt(int index, int value) {
                            if (index < 0 || index > size)

                                              throw new IndexOutOfBoundsException("Index: " + index + ", Size: "

                                                         + size);

                                        ensureCapacity(size + 1);

                                        int moveCount = size - index;

                                        if (moveCount > 0)

                                              System.arraycopy(storage, index, storage, index + 1, moveCount);

                                        storage[index] = value;

                                        size++;


                        }

                        public void add(int value) {
                    if (size== storage.length)
                    {
                        ensureCapacity ((size+1)*2);
                        }
                        storage[size]= value;
                        size++;
                    }
                        public void removeAt(int index) {
                           rangeCheck(index);

                                       int moveCount = size - index - 1;

                                       if (moveCount > 0)

                                             System.arraycopy(storage, index + 1, storage, index, moveCount);

                                       size--;


                        }

                        public void printAll() {
                    for (int i= 0; i < size; i++) {
                                 System.out.printf("  [%d]=%d", i, get(i));
                            }

                          System.out.println();
                       }





                        public static void main(String args[]) {
                           DynamicArrayOfInts list1 = new DynamicArrayOfInts();
                           list1.insertAt(0,1);
                           list1.insertAt(1,2);
                           list1.add(3);
                           // list1 is 1, 2, 3
                           System.out.print("list1: "); list1.printAll();
                           list1.set(2,100);
                           // list1 1 is 1, 2, 100
                           //System.out.print("list1: "); list1.printAll();
                           System.out.println("list1[2]=" + list1.get(2));
                           list1.removeAt(2);
                           // list1 is 1, 2
                           System.out.print("list1: "); list1.printAll();
                           DynamicArrayOfInts list2 = new DynamicArrayOfInts();
                           list2.insertAt(0,2);
                           list2.insertAt(0,3);
                           list2.insertAt(0,1);
                           list2.removeAt(1);
                           // list2 is 1, 2
                           System.out.print("list2: ");list2.printAll();
                           System.out.println("list1.size()=" + list1.size() + ", list2.size()=" + list2.size());
                           // list1 and list2 are equal
                           System.out.println("list1 equals list2 is " + list1.equals(list2));
                           list2.insertAt(2,3);
                           // list2 is 1, 2, 3
                          System.out.println("list1.size()=" + list1.size() + ", list2.size()=" + list2.size());
                           // list1 and list2 are not equal
                           System.out.println("list1 equals list2 is " + list1.equals(list2));
                             ArrayList list3 = new ArrayList();
                             list3.add(1);list3.add(2);list3.add(3);
                             System.out.println("list2 equals list3 is " + list2.equals(list3));
                        }
                    }
                     im getting this error C:\Users\papa\Desktop\DynamicArrayOfInts.java:150: cannot find symbol
                symbol  : class ArrayList
                location: class DynamicArrayOfInts
                         ArrayList list3 = new ArrayList();
                         ^
                C:\Users\papa\Desktop\DynamicArrayOfInts.java:150: cannot fin

                d symbol
                symbol  : class ArrayList
                location: class DynamicArrayOfInts
                         ArrayList list3 = new ArrayList();
                                               ^
                2 errors

                Tool completed with exit code 1
                2nd file



                import java.util.ArrayList;
                import java.util.Arrays;
                import java.util.LinkedList;
                import java.util.List;
                import java.util.ListIterator;
                import java.util.Random;
                import java.util.Vector;

                public class PA1 {

                        static class ExecutionResults {
                                int size;
                                long linkedListTime;
                                long arrayListTime;
                                long dynamicArrayTime;

                                public ExecutionResults(int size) {
                                        this.size = size;
                                }
                        }

                        public static void main(String[] args) throws Exception {

                                int choice = 4;
                                if (args.length >0)
                                         choice = Integer.parseInt(args[0]);

                                     // warm up the JVM
                                for (int i = 0; i < 100; i++)
                                   CheckPerformance(1000,choice);

                                // print header
                                if (choice == 4)
                                    System.out.printf("%-15s %-20s %-20s %-20s %n",
                                      "size",  "LinkedList", "ArrayList",      "Dynamic Array");

                                // perform the benchmarking
                                displayResult(CheckPerformance(100,choice),choice);
                                displayResult(CheckPerformance(200,choice),choice);
                                displayResult(CheckPerformance(500,choice),choice);
                                displayResult(CheckPerformance(1000,choice),choice);
                                displayResult(CheckPerformance(2000,choice),choice);
                                displayResult(CheckPerformance(3000,choice),choice);
                                displayResult(CheckPerformance(4000,choice),choice);
                                displayResult(CheckPerformance(8000,choice),choice);
                                displayResult(CheckPerformance(16000,choice),choice);
                        }

                        private static void displayResult(ExecutionResults r, int choice) {

                                long min = 1;
                                if (choice == 4) {
                                    min = Math.min(r.linkedListTime, Math.min(r.arrayListTime, r.dynamicArrayTime));

                                    System.out.printf("%-15d %-10d (%-3.0f%%)    %-10d (%-3.0f%%)     %-10d (%-3.0f%%)%n",
                                                r.size,
                                                r.linkedListTime,   (100 * (double) r.linkedListTime) / min,
                                                r.arrayListTime,    (100 * (double) r.arrayListTime) / min,
                                                r.dynamicArrayTime, (100 * (double) r.dynamicArrayTime) / min);
                                }
                        }
                `Inline Code Example Here`
                        private static ExecutionResults CheckPerformance(int size, int choice) {

                                Integer[] array = new Integer[size];
                                Random random = new Random(123456789L);

                                LinkedList<Integer> linkedList = new LinkedList<Integer>(Arrays.asList(-1));
                                ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(-1));
                                DynamicIntArray dynamicArray = new DynamicIntArray();
                                dynamicArray.add(-1);

                                for (int i = 0; i < array.length; i++)
                                        array[i] = random.nextInt(Integer.MAX_VALUE);

                                ExecutionResults result = new ExecutionResults(size);

                                     switch (choice) {
                                case 1:
                                    long before = System.nanoTime();
                                 insertIntoLinkedList(array, linkedList);
                                  result.linkedListTime = (System.nanoTime() - before) / 1000;
                                  break;
                                     case 2:
                                    before = System.nanoTime();
                                 insertIntoArrayList(array, arrayList);
                                  result.arrayListTime = (System.nanoTime() - before) / 1000;
                                       break;
                                     case 3:
                                    before = System.nanoTime();
                                 insertIntoDynamicArray(array, dynamicArray);
                                  result.dynamicArrayTime = (System.nanoTime() - before) / 1000;
                                  break;
                                 case 4:
                                    before = System.nanoTime();
                                 insertIntoLinkedList(array, linkedList);
                                  result.linkedListTime = (System.nanoTime() - before) / 1000;

                                    before = System.nanoTime();
                                 insertIntoArrayList(array, arrayList);
                                  result.arrayListTime = (System.nanoTime() - before) / 1000;

                                    before = System.nanoTime();
                                 insertIntoDynamicArray(array, dynamicArray);
                                  result.dynamicArrayTime = (System.nanoTime() - before) / 1000;
                                 }

                                // check that they are equal
                                if (choice == 4 && !(linkedList.equals(arrayList) && dynamicArray.equals(arrayList)))
                                        throw new RuntimeException("Lists not equal...");

                                return result;
                        }


                        private static void insertIntoLinkedList(Integer[] intArray, LinkedList<Integer> list) {
                              for (Integer integer : intArray) {
                                                    for (ListIterator<Integer> it = list.listIterator(); it.hasNext();) {
                                                            if (integer.compareTo(it.next()) >= 0) {
                                                                    it.previous(); // should be added before element
                                                                    it.add(integer);
                                                                    break;
                                                            }
                                                    }
                                            }

                        }

                        private static void insertIntoArrayList(Integer[] intArray, ArrayList<Integer> list) {
                            for (Integer integer : intArray) {
                                                    int list_size = list.size();  // on purpose: it is smarter to avoid calling size() every loop
                                                    for (int i = 0; i < list_size; i++) {
                                                            if (integer.compareTo(list.get(i)) >= 0) {
                                                                    list.add(i, integer);
                                                        break;

                        }
                    }
                }
                }

                         private static void insertIntoDynamicArray(Integer[] numbers, DynamicIntArray array) {
                  for(Integer integer : numbers){
                        int value = integer.intValue();
                         //        int array_size = array.size();           // on purpose: even faster would be to avoid calling size() every loop
                        for(int idx = 0; idx < array.size(); idx++){
                          if(value >= array.get(idx)){
                            array.insertAt(idx, value);
                            break;
                          }
                        }
                      }
                    }
                          }
                          here i am getting following error.i know i have alot of codes
                          C:\Users\papa\Desktop\PA1.java:144: cannot find symbol
                symbol  : class DynamicIntArray
                location: class PA1
                         private static void insertIntoDynamicArray(Integer[] numbers, DynamicIntArray array) {
                                                                                       ^
                C:\Users\papa\Desktop\PA1.java:72: cannot find symbol
                symbol  : class DynamicIntArray
                location: class PA1
                                DynamicIntArray dynamicArray = new DynamicIntArray();
                                ^
                C:\Users\papa\Desktop\PA1.java:72: cannot find symbol
                symbol  : class DynamicIntArray
                location: class PA1
                                DynamicIntArray dynamicArray = new DynamicIntArray();
                                                                   ^
                3 errors

                Tool completed with exit code 1








    `Inline Code Example Here`


    http://www.daniweb.com/software-development/java/9/contribute# 

not sure why getting error

Please copy the full text of the error message and paste here.

symbol : class ArrayList
location: class DynamicArrayOfInts
ArrayList list3 = new ArrayList();

Right there. You need to import ArrayList to your program. At the top of the program where you have import java....; lines in your DynamicArrayOfInts class, add one more line as import java.util.ArrayList; to the import...

Edited 4 Years Ago by Taywin

Thanks in my PA1 file i am still getting following error
C:\Users\papa\Desktop\PA1.java:144: cannot find symbol
symbol  : class DynamicIntArray
location: class PA1
         private static void insertIntoDynamicArray(Integer[] numbers, DynamicIntArray array) {
                                                                       ^
C:\Users\papa\Desktop\PA1.java:72: cannot find symbol
symbol  : class DynamicIntArray
location: class PA1
                DynamicIntArray dynamicArray = new DynamicIntArray();
                ^
C:\Users\papa\Desktop\PA1.java:72: cannot find symbol
symbol  : class DynamicIntArray
location: class PA1
                DynamicIntArray dynamicArray = new DynamicIntArray();
                                                   ^
3 errors

Tool completed with exit code 1

The compiler can not find the definition for the DynamicIntArray class.
Is the class definition for that class on the classpath when you try to compile the PA1.java file?
Where is the definition for the DynamicIntArray class? Does it have a constructor without any args?

Edited 4 Years Ago by NormR1

The OP dumped every thing in the first post in the code tag... I didn't see that the ending of the code has this error as well...

Your error is from using wrong class name. You define DynamicArrayOfInts in your code, but you use DynamicIntArray instead? Der...

This article has been dead for over six months. Start a new discussion instead.