4

In the code below I am trying to avoid the last three lines which allocate memory for the instances of class. Any suggestions on how to bring the memory allocation part inside the class definition? So what I want to do is to be able to execute pInfo[0].sValue="string"; right after AClass [] pInfo = new AClass[10];

  class AClass {
     private String sName="";
     private String sValue="";
  }

    AClass [] pInfo = new AClass[10];

   // how to avoid the code below or bring them into class definition?  

    pInfo[0] = new AClass();
    pInfo[1] = new AClass();
      ... 
    pInfo[9] = new AClass();

EDIT: what I mean by efficiency is in the amount of code + code readability

3
  • 1
    Do you mean typing efficient or performance efficient? Commented Jul 31, 2012 at 20:20
  • It might be nicer to take advantage of constructor arguments to initialize the fields, so you can write it like: AClass[] pInfo = new AClass[] { new AClass("name-1", "value-1"), ... }; Commented Jul 31, 2012 at 20:24
  • @Yogu in the amount of code + code readability Commented Jul 31, 2012 at 20:28

4 Answers 4

7
AClass[] pInfo = {new AClass(),new AClass(), etc.};

OR

AClass[] pInfo = new AClass[10];

for(int i = 0; i < pInfo.length; i++)  
{  
    pInfo[i] = new AClass();  
}  
Sign up to request clarification or add additional context in comments.

Comments

1

There is no way to avoid that, you will need to explicitly assign a value to each element of your array.

JLS §10.3 states that arrays provide initial values for their elements when they are created.

JLS §4.12.5 states that the initial value for reference types is null.

Comments

1

You can try something like this:

class AClass {
     public String sName="";
     public String sValue="";
}

class AClassArray {
     public AClass[] pInfo;

     public AClassArray() {
        pInfo = new AClass[10];
        for(int i = 0; i < pInfo.length; i++)  
           pInfo[i] = new AClass();  
     }
}

Use:

AClassArray aClassArray = new AclassArray();
aClassArray.pInfo[i].sXXXX;

6 Comments

@Woot4Moo: Hit submit too soon
This works, but isn't very extensible. You might as well use a Vector or List rather than reinventing the wheel.
@Code-Guru: Of course not! The underlying data structure is an array!
@DougRamsey What if I want to create BClass and have an array of this new class? Now I will have to write a BClassArray. On the other hand, a generic collection such as Vector or List will work with any class I choose without writing a customized Array class for it. (Perhaps "extensible" isn't quite the right word to describe this issue...)
@DougRamsey That may be...I'm not up on all the language changes since Java 5 ;-(
|
0
class AClass {
   private String sName="";
   private String sValue="";

   public static final AClass[] getArrayOfObjs(int size){
    if(size <= 0)
       return null; //You can also create your custom exception to be thrown here
    AClass[] array = new AClass[size];

    for(int i = 0; i < size; i++)  
    {  
       array[i] = new AClass();  
    }  
    return array;
  }
}

Then instantiate as

AClass [] pInfo = AClass.getArrayOfObjs(10);

1 Comment

if size < 0, this object can be exploited because it will throw an exception and is not final.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.