Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a java code like below.
myclass a1 = new myclass(p1,p2); myclass a2 = new myclass(p3,p4);
I want to do something like
myclass a[1] = new myclass(p1,p2); myclass a[2] = new myclass(p3,p4);
how to do it ?
myclass[] a = new myclass[2];
a[0] = new myclass(p1, p2); a[1] = new myclass(p3, p4);
myclass[] myArray = new myclass[5]; myArray[0] = new myclass(p1,p3)
This is valid in java.
Add a comment
Try this:
myclass[] a = new myclass[]{ new myclass(p1,p2), new myclass(p1,p2) };
it could be done in one line:
myclass[] myArray = myclass[]{ new myclass(p1, p2), new myclass(p3, p4) };
This is the same as:
myclass[] myArray = new myclass[2]; myArray[0] = new myclass(p1, p2); myArray[1] = new myclass(p3, p4);
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
myclass[] a = new myclass[2];and thena[0] = new myclass(p1, p2); a[1] = new myclass(p3, p4);.