+ 1
Guys is there a way to do something like this (Java) String[ ] myNames ; myNames[] = { "A", "B", "C", "D"};
I can do this in c++ but I don't know how to do this in java
6 odpowiedzi
+ 3
You have to use new keyword in Java:
public class Program {
public static void main(String[] args) {
//Initialize array first:
String[ ] myNames;
//Assaing value later:
myNames = new String[]{"A", "B", "C"};
System.out.println(myNames[2]);
//Output: C
}
}
+ 1
JBK 02 if you look at my answer better, you will see that I first define the string and assign the values later!
Java is not C++ therefore it is needed to use the keyword “new” when assign values to an array previously declared!
The only other way is that you define the array size when initializing it and after you can assaing values to it by indexing, that is:
String[] myNames = new String[3];
myNames[0] = “A”;
But what you asked is what I wrote in my answer before!
+ 1
Sorry man, I didn't notice that. Thanks for that.
+ 1
JBK 02 Dont worry! Just glad I could help 😉
0
I know these will work . see what I asked properly, I want to define the string first without assigning values and when I want to assign values i don't know how to do.
- 1
try
myNames={"A","B","C",D"};