You need to initialize errorSoon, as indicated by the error message, you have only declared it.
Use with AIString[] errorSoon; // <--declared statement
String[] errorSoon = new String[100]; // <--initialized statement
You need to initialize the array so it can allocate the correct memory storage for the String elements before you can start setting the index.
If you only declare the array (as you did) there is no memory allocated for the String elements, but only a reference handle to errorSoon, and will throw an error when you try to initialize a variable at any index.
As a side note, you could also initialize the String array inside braces, { } as so,
Use with AIString[] errorSoon = {"Hello", "World"};
which is equivalent to
Use with AIString[] errorSoon = new String[2];
errorSoon[0] = "Hello";
errorSoon[1] = "World";
Answer from Anthony Forloney on Stack OverflowYou need to initialize errorSoon, as indicated by the error message, you have only declared it.
Use with AIString[] errorSoon; // <--declared statement
String[] errorSoon = new String[100]; // <--initialized statement
You need to initialize the array so it can allocate the correct memory storage for the String elements before you can start setting the index.
If you only declare the array (as you did) there is no memory allocated for the String elements, but only a reference handle to errorSoon, and will throw an error when you try to initialize a variable at any index.
As a side note, you could also initialize the String array inside braces, { } as so,
Use with AIString[] errorSoon = {"Hello", "World"};
which is equivalent to
Use with AIString[] errorSoon = new String[2];
errorSoon[0] = "Hello";
errorSoon[1] = "World";
Use with AIString[] args = new String[]{"firstarg", "secondarg", "thirdarg"};
You could do something like this
String[] myStrings = { "One", "Two", "Three" };
or in expression
functionCall(new String[] { "One", "Two", "Three" });
or
String myStrings[];
myStrings = new String[] { "One", "Two", "Three" };
By using the array initializer list syntax, ie:
String myArray[] = { "one", "two", "three" };
You have forgotten to give the variable a name? Both of these work.
Use with AIString[][] array = {emptyArr, filledArr, moreFilledArr};
Use with AIString[][] array = new String[][] {emptyArr, filledArr, moreFilledArr};
I recommend you to go through the basic Java syntax specification and tutorials. Start with The Java Tutorials by Oracle Corp, free of cost.
these two are right too String[][] array = {emptyArr, filledArr, moreFilledArr};
Use with AIString[][] array = new String[][] {emptyArr, filledArr, moreFilledArr};
please do check these too
Docs for Array in Java Have a Look for more clarification
tutorial of array with java docs
,
I had to add this as a comment but I couldn't, I want to extend with more resources mentioned above , Thanks
Use this code:
String[] Year = new String[50]; //Initialize the year string
Now Java knows how long you want the array to be, so it can initialize it.
If you don't tell Java how long you want your array to be, it can't make the array because it doesn't know how long the array should be.
Also, you could use a final variable SIZE if you want to adjust it easily.
Furthermore, you don't need the string j since you only use it to store the result of something before adding it to the array.
This will not affect your code, but the correct Java naming convention is that variables should start with a lowercase letter, so change Year to years.
Therefore, your code could be improved like this:
int year = 1995; //the current year i'm working with
final int SIZE = 50; //how big
String[] years = new String[SIZE]; //Initialize the year string
for(int i=(year-SIZE); i < year; i++) //Note the use of the SIZE variable
{
years[i] = Integer.toString(i);
}
U didn;t Initilize Year Varible, also, there is not Items inside the Year Array
String[] Year = new String[50]; // Initlaize adn add Some content
There are various options. Personally I like using Guava:
List<String> strings = Lists.newArrayList("s1", "s2", "s3");
(Guava's a library worth having anyway, of course :)
Using just the JDK, you could use:
List<String> strings = Arrays.asList("s1", "s2", "s3");
Note that this will return an ArrayList, but that's not the normal java.util.ArrayList - it's an internal one which is mutable but fixed-size.
Personally I prefer the Guava version as it makes it clear what's going on (the list implementation which will be returned). It's also still clear what's going on if you statically import the method:
// import static com.google.common.collect.Lists.newArrayList;
List<String> strings = newArrayList("s1", "s2", "s3");
... whereas if you statically import asList it looks a little odder.
Another Guava option, if you don't want a modifiable-in-any-way list:
ImmutableList<String> strings = ImmutableList.of("s1", "s2", "s3");
I typically want to either have a completely mutable list (in which case Lists.newArrayList is best) or a completely immutable list (in which case ImmutableList.of is best). It's rare that I really want a mutable-but-fixed-size list.
Java 9+
Java 9 introduces a convenience method List.of used as follows:
List<String> l = List.of("s1", "s2", "s3");
Java 8 and older
Here are a few alternatives:
// Short, but the resulting list is fixed size.
List<String> list1 = Arrays.asList("s1", "s2", "s3");
// Similar to above, but the resulting list can grow.
List<String> list2 = new ArrayList<>(Arrays.asList("s1", "s2", "s3"));
// Using initialization block. Useful if you need to "compute" the strings.
List<String> list3 = new ArrayList<String>() {{
add("s1");
add("s2");
add("s3");
}};
When it comes to arrays, you could initialize it at the point of declaration like this:
String[] arr = { "s1", "s2", "s3" };
If you need to reinitialize it or create it without storing it in a variable, you do
new String[] { "s1", "s2", "s3" }
If the string constants are may though, it would look like
String[] arr = { "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10",
"s11", "s12", "s13" };
In these cases I usually prefer writing
String[] arr = "s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13".split(",");
Because you can not define dimension expressions when an array initializer is provided.
That means that you can either specify the dimension or the array initializer (i.e. the values). You can't specify both at the same time.
Your second initalization is the correct one.
The first syntax is not valid because you are declaring the size of the array twice:
- once explicitly
- another time because the array of strings between { and } has size 1
Doing the same thing twice can introduce potential errors so the java designers decided to permit only the second approach.