FAQ: Learn Java: Arrays - Creating an Empty Array

This community-built FAQ covers the “Creating an Empty Array” exercise from the lesson “Learn Java: Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Creating an Empty Array

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

So far we’ve only seen arrays that store one type of data. Is there another object that can group instances of multiple types?

2 Likes
String[] favoriteArticles;
  
  public Newsfeed(){
    // Initialize favoriteArticles here:
String[] favoriteArticles = new String[10];
  }

Gives nullpointer exception.

String[] favoriteArticles = new String[10];
  
  public Newsfeed(){
    // Initialize favoriteArticles here:
  }

Works

5 Likes

The way to initialize within the constructor method is as follows:

String favoriteArticles;

public Newsfeed(){
// Initialize favoriteArticles here:
favoriteArticles = new String[10];
}

Since the array has already been assigned String outside of the constructor, you can omit String within constructor. Within constructor favoriteArticles = new String[10]; will work without returning any exceptions.

11 Likes

public void setFavoriteArticle(int favoriteIndex, String newArticle){
// Add newArticle to favoriteArticles:
favoriteArticles[favoriteIndex] = newArticle;

}

I fail to understand how favoriteIndex plays in to this line of code and how it changes the behaviour of the Array

3 Likes

This Code works as well

String topics = {“Opinion”, “Tech”, “Science”, “Health”};
int views = {0, 0, 0, 0};
String favoriteArticles;

public Newsfeed(){
// Initialize favoriteArticles here:
favoriteArticles = new String[10];
}

What does the second part mean. I have tried linking to the array, but it gives error

1 Like

Question 1)
This is how I changed the constructor:

// The " Newsfeed" constructor
  public Newsfeed(){
    // Initialize favoriteArticles here:
    favoriteArticles = new String[10];
    
  }

The output was:

[null, null, null, null, null, null, null, null, null, null]

which is consistent, an array with 10 " locations ", ranging from index = 0 to index = 9

1 Like

Question 2)
Here is how I changed the setFavoriteArticle() method:

public void setFavoriteArticle(int favoriteIndex, String newArticle){
    // Add newArticle to favoriteArticles:
    
    favoriteArticles[favoriteIndex] = newArticle;
    
  }

The variable favoriteArticles[favoriteIndex] will evaluate to a String value because the array " favoriteArticles " is just an array full of String values (the name of the articles in this case). Then we can change this String value to a different String value without any errors. The exercise wants us to change the String value already in the array to the String value that the parameter " newArticle " evaluates to.

We want to use the parameters we are supplying it, we want to do something with the information we are feeding the program in the main() method. Why bother supplying the program with input/data if we aren’t gonna do anything with it.

The final array looked like this in the output:

[Oil News, null, Humans: Exterminate Or Not?, Organic Eye Implants, null, null, null, null, null, null]

This is consistent with the three setFavoriteArticle() method calls written inside of the main() method. The first call line we are trying to change the article name at index value 2 (which would be the third item in the array). The second call line we are trying to change the article name at index value 3 (which would be the fourth item in the array). The third and last call line we are trying to change the article name at index value 0 (which would be the very first item in the array). And the last line in the main() method simply prints out the new array that we have manipulated in all that code we wrote for this exercise. Remember the array was empty to begin with it looked like this:

[null, null, null, null, null, null, null, null, null, null]

The point of our program was to fill in the empty array with the data/parameters supplied inside the main() method.

1 Like

It’s so that when we call up this method using

sampleFeed.setFavoriteArticle(2, “Humans: Exterminate Or Not?”);

we are saying that for this particular case the variable favoriteIndex = 2
which when put into favoriteArticles[favoriteIndex] = newArticle;
assigns the 2nd index of favoriteArticles to the String newArticle;
This seems a little useless for a program of this size but I think the purpose is to prepare us for the usefulness of this when working on more complex projects.

Can anyone tell me why have we initialized the empty array in the constructor method and why not outside it?