简体   繁体   中英

initializing a member variable instead of an instance variable

I have been having issues with a class named arrayList that represents a list of objects and supports random access to its objects via a numeric position index.

After working out the issues with toString() and size() , I encountered an issue where I am not initializing my member array; only init'ing a locale variable. However I was under the impression that a modification I made should have solved the issue.

public Object get(int a) {
    if (a < 0 || a >= logicalSize) {
        throw new IndexOutOfBoundsException("Positions must be from position 0 to position "+
                                            (logicalSize - 1));
    }
    else {
        return array[a];
    }
} 

But shouldn't my declaration of private static Object[] array = new Object[5]; solve that?

I am currently getting an error throwing my exception, telling me my position must be from 0 to -1, suggesting it isn't created.

Full Code:

public class Tester  {
        public static void main(String [] args)
        {
            arrayList a1, a2;
            a1 = new arrayList();
            a2 = new arrayList(5);
            a2.size();
            System.out.println(a1.toString());
            //System.out.println(a2.toString());
        }
    }

public class arrayList
{
    private int logicalSize;
    private static Object[] array = new Object[0];
    private Object[] original;
    private Object removedElement;

    public arrayList()
    {
        Object[] array = new Object[]{null,null,null,null,null}; 
    }

    public arrayList(int i)
    {
        logicalSize = i;
        Object[] array = new Object[logicalSize - 1];
    }

    public arrayList(Object[] array)
    {
       logicalSize = array.length;
       Object[] copyArray = array;
    }

    public String toString(Object[] array)
    {
       String str = " ";

       for(int a = 0; a < logicalSize; a++)
       {
          str = str + array[a];
       }

       str = str + "\nSize: " + size();
      return str;
   }  

   public int size()
   {
       int length = array.length;
       return length;
   }
}

Much Thanks, Packerfan504

First, you should follow Java conventions for class names. It should start with a capital letter and go CamelCase.

array should certainly not be static . When you declare a member static , it means that it is shared between all instances

In the Constructors, you set local variables instead of the member.

 // remove Object[] in front
 array = new Object[]{null,null,null,null,null};

The constructor from an array does not set the member array .

public arrayList(Object[] array)
{
   logicalSize = array.length;
   //Object[] copyArray = array;
   this.array = array;
}

This does not do any copy: the member reference the array passed as a parameter. I you want some kind of "copy constructor", you need to actually instantiate a new array and copy each element. Or use Arrays.copyOf() .

You should overide toString() method from java.langObject with right signature (why pass the array as a parmeter?). All classes in Java extends implicitly Object class.

You should differentiate the Object[] array size - which is the number of potenital slots you have before needing an increase of the array, and the logical size that reflects the number of elements you have put in your array. Here you set the logical size to array.length, thats just redundant information.


EDIT Additional advices

In the constructor with the size as parameter, you create an array of (logicalSize-1). Why?

In the default constructor, you don't initialize logicalSize , it is then set to 0 . It's ok for me, but then, why do you set it to i in the constructor with a size as parameter? Then it's normal that in the a1.get(0) :

if (a < 0 || a >= logicalSize) {...}

is true and throw the exception.

I would recommend that you clarify the role of logicalSize which is (for me) the number of actual objects in the ArrayList . Note that in your test, you don't put anything in your array yet. Even if you have an internal array that can hold 5 Objects, until you add(...) something, calling get(0) should throw an exception.

You have a class static variable

private static Object[] array 

and in your constructor you are creating a local variable array;

ie doing this

Object[] array 

is again going to create a local variable for that function.

since you have declared it Object[] again its going to be a variable within the constructor scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM