+ 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

7th Jan 2019, 2:19 AM
BALA KUMARAN J
BALA KUMARAN J - avatar
6 Réponses
+ 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 } }
7th Jan 2019, 2:55 AM
Sekiro
Sekiro - avatar
+ 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!
7th Jan 2019, 1:56 PM
Sekiro
Sekiro - avatar
+ 1
Sorry man, I didn't notice that. Thanks for that.
8th Jan 2019, 2:03 AM
BALA KUMARAN J
BALA KUMARAN J - avatar
+ 1
JBK 02 Dont worry! Just glad I could help 😉
8th Jan 2019, 3:07 AM
Sekiro
Sekiro - avatar
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.
7th Jan 2019, 1:32 PM
BALA KUMARAN J
BALA KUMARAN J - avatar
- 1
try myNames={"A","B","C",D"};
7th Jan 2019, 2:53 AM
Pallav W
Pallav W - avatar