For basic arrays, we can directly combine declaration and initialization as for eg: int[] num= new int[2]
But can we do this do for forming class object arrays?
like
class stu
{
.....
}
stu[] s=new stu[2]
????
please help. I am noob 2 java...
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
For basic arrays, we can directly combine declaration and initialization as for eg: int[] num= new int[2]
But can we do this do for forming class object arrays?
like
class stu
{
.....
}
stu[] s=new stu[2]
????
please help. I am noob 2 java...
Yes, you can make an array of objects.
Remember code goes in between code tags =)
crisdeodates (September 18th, 2014)
Thanks Hamenpoi,
But I didnt get your point. I am new to java. Could you explain with an example?
Thanks
Also for arrays of objects, the array definition only creates slots for objects, it does not assign values to any of those slots. The code will need to do that in separate steps.
If you don't understand my answer, don't ignore it, ask a question.
crisdeodates (September 18th, 2014)
public class MathEntity extends Entity { private MathEntity results[] = new MathEntity[3]; private int result; public MathEntity() { id = EntityIDList.MATHENTITY; } public MathEntity(int val) { this(); name = Integer.toString(val); } private void setAnswers() { results[0] = new MathEntity(result); results[1] = new MathEntity(wrongAnswer(result)); results[2] = new MathEntity(wrongAnswer(result)); }
Do you mean like this? It's no where near optimal but neither am I.