FAQ: Learn Java: Arrays - Creating an Empty Array



As I mentioned, you are assigning newArticle the value of favoriteArticles[favoriteIndex], which is not what you want. You want favoriteArticles[favoriteIndex] to be assigned the value of newArticle.

Take a look at my previous explanation:

Now really consider these two points above. If you are not assigning any value to a certain element in your array, and the array was instantiated (meaning its elements were assigned default values), why do you think printing out favoriteArticles results in all null values?

Hi, thank you for replying again and trying to help me, you explained it very well but it is still confusing to me T_T.

What I understand is that the value on the right of the (=) is assigned to the object. In the original code, newArticle is a parameter for setFavoriteArticle method, when I write the code correctly , favoriteArticles[favoriteIndex] = newArticle, it assigns the value that I am going to give to newArticle to whatever index I choose, but if I write it in reverse, newArticle doesn’t get any value assigned and it outputs null because it is only the index assigned and not a String .

Yes, correct.

You’re off here. newArticle is on the left of the assignment operator (=) on the line:

newArticle = favoriteArticles[favoriteIndex]

This means that newArticle is assigned the value of the element at the favoriteIndex in the array favoriteArticles. However, what you want to do is assign the value of newArticle to the specified index in favoriteArticles.

This is false. Firstly, you aren’t assigning anything to favoriteArticles[favoriteIndex] since you’re assigning something to newArticle on the line newArticle = favoriteArticles[favoriteIndex]. So, the point is moot. Secondly, favoriteArticles[favoriteIndex] is not an index, but rather the element at the specified index. You can’t assign an index (which is of type int) to a String array.

Main Point

Because you aren’t assigning anything to favoriteArticles[favoriteIndex], the element at that index of the array remains the default value (null) and is not overwritten by the value of newArticle. Here’s an example.

Let’s say I called the replaceFirst method like so.

String[] names = {"Abigail", "Bob", "Chris", "Deborah"};

replaceFirst(names, "Anne");

Where the following is the replaceFirst method.

public static void replaceFirst(String[] myArray, String replacement) {
  replacement = myArray[0]; // assigns replacement the value of "Abigail"
  System.out.println(myArray); // prints ["Abigail", "Bob", "Chris", "Deborah"]
  System.out.println(replacement); // prints Abigail

Now let’s say I used the same method call but the method was written like so.

public static void replaceFirst(String[] myArray, String replacement) {
  myArray[0] = replacement; // assigns the first element in myArray to "Anne"
  System.out.println(myArray); // prints ["Anne", "Bob", "Chris", "Deborah"]
  System.out.println(replacement); // prints Anne

Reread every response I’ve written and piece it all together. If you are still unable to understand, I recommend you restart the entire course since variable assignment is one of the most elementary concepts you need to understand before going any further.

Thank you so much, I will pause this course and read whatever I find on variable assignment.

1 Like