Results 1 to 6 of 6

Thread: MyArrayList

  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default MyArrayList

    I'm experimenting with implementation of my own ArrayList class, called MyArrayList.
    There is a method called add, and while doing it, wonder if I'm on the right way, or should have different approach?

    Java Code:
        public void add(Object o) {      
            add(size, o);        
        }
        
        public void add(int index, Object o) {
            if (index > size) {
                System.err.print("Index out of bounds");
                System.exit(size - index);
            }     
            
            if (size == capacity) {    
                capacity *= 2;            
                stack = Arrays.copyOf(stack, capacity);                     
            }                           
            
            if (index != size) {
                Object[] temp = Arrays.copyOf(stack, size);
                System.arraycopy(temp, index, stack, index + 1, size - index);
            }
                
            stack[index] = o;                
            size++;
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: MyArrayList

    Wonder how Arrays class don't have method such is arraycopy?

    Java Code:
        public void add(int index, Object o) {
             
            try {
                if (size == capacity) {    
                    capacity *= 2;            
                    stack = Arrays.copyOf(stack, capacity);                     
                }                           
            
                if (index != size) {
                    Object[] temp = Arrays.copyOf(stack, size);
                    System.arraycopy(temp, index, stack, index + 1, size - index);
                }
                
                stack[index] = o;                
                size++;            
            }
            catch (IndexOutOfBoundsException e) {    
                //
            }
        }
    Last edited by diamonddragon; 01-31-2012 at 12:29 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: MyArrayList

    You will want to check into throwing an exception if something is amiss. :) Perhaps a new Exception class that you've created to go along with this class.

  5. #5
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: MyArrayList

    I assume that means I'm on the right way.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: MyArrayList

    I meant something like this:

    Java Code:
    public class MyArrayList throws MyArrayListException {
      public void add(int index, Object o) {
        if (index > size) {
            throw new MyArrayListExcetion("index " + index + " is > size");
        }
    Then create a class MyArrayListException that extends Exception. You should read the Exceptions section of the tutorials to learn more. This will allow classes that use your class decide what to do if the exception is thrown. The outside classes can exit the program or can try to fix the error.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •