for(int i=1;i<6;i++)
Zerg zergi = new Zerg();
why can i not do like thatcan i do
Resolved
Comments (15)
- Popular
- New
- Old
You must be signed in to leave a comment
hidden #10443785
1 May 2019, 18:14
because you have to give unique names to all objects
0
Dman
30 April 2019, 10:16
I tried the same thing actually:
// for (int i = 1; i <= 5; i++) {
// Zerg z+i = new Zerg();
// z+i.name = "Zerg" + i;
}
I couldn't quite get the syntax right to resolve the "z+I" or it was failing in anycase. Did you continue with your Java journey in the end?
0
P.B.Kalyan Krishna
5 November 2018, 09:25
Well you can certainly do like that, but in our course we are yet to be introduced to LOOP concepts. So using what ever we learnt upto that point, I didn't use Loops and did it in a primitive way.
0
Ashish RajAnand
5 November 2018, 15:40
How are you write table 1 to 10
by printing one by one number .
I use simple nested loop to print that .
If you don't know how to use please follow this link
Looping concept
0
P.B.Kalyan Krishna
6 November 2018, 02:13
for(int i=1;i<=10;i++)
{
for(int j=i,k=0;k <=10;j++,k++)
{
System.out.print(i * j +" ");
}
System.out.println();
}
// I didn't see your solution. You can see my solution in nested loops here. Thanks!!!
0
Ashish RajAnand
6 November 2018, 02:56
why did you tell me that i can't use loop concept here?
+1
hidden #10366080
4 November 2018, 15:35
the loop is creating the same object over and over again
0
Ashish RajAnand
4 November 2018, 15:47
thanks
0
Ashish RajAnand
5 November 2018, 15:42
please explain in details
0
hidden #10366080
5 November 2018, 22:01
according to my knowledge, you have to create a class for Zerg, then zergi will be the object of that class. example i can create a class of Car, then to create an object for the Car class, i'd say
Car honda = new Car(); and creating an object after a loop is bad programing practice which will generate an error unless it is possible in an advanced section.
0
Ashish RajAnand
6 November 2018, 03:23
+1
Ashish RajAnand
6 November 2018, 03:24
It is not worng and it work
0
hidden #10366080
6 November 2018, 04:59
you did exactly what i pointed out in my reply, but to reiterate, i've never seen a code like that before because you did not need a class for this code, it is a waste of memory, plus as someone starting out with java i don't see the point to it. but to each their own
0
Ashish RajAnand
6 November 2018, 05:09
thank you
helpful
+2
Jeremiah Samuel Krakowski
30 December 2018, 04:20
It's cool sure but unnecessary. and I agree, added memory is a concern.
+1