FAQ: Learn Java: Arrays - Get Element By Index

This community-built FAQ covers the “Get Element By Index” 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 Get Element By Index

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!

I dont understand this at all. How does views[topicIndex] reference an actual index? Where is it getting the index number from?

public void viewTopic(int topicIndex){
views[topicIndex] = views[topicIndex] + 1;


heya sirfency! from what i realised by comparing the viewTopic method to getTopTopic, it pretty much takes the view array at the start of the class, selects an index according to what you put in the parameters when you call the method, ands adds +1 to it and stores it in the array. hope this helped!

import java.util.Arrays;

public class Newsfeed {
  String[] topics = {"Opinion", "Tech", "Science", "Health"};
  int[] views = {0, 0, 0, 0};
  // constructor class
  public Newsfeed(){

  public String[] getTopics(){
    return topics;
  public String getTopTopic(){
    String indexZero = topics[0];
    return indexZero;
  public void viewTopic(int topicIndex){
    views[topicIndex] = views[topicIndex]+1;

  public static void main(String[] args){
    Newsfeed sampleFeed = new Newsfeed();
    System.out.println("The top topic is "+ sampleFeed.getTopTopic());
    System.out.println("The " + sampleFeed.topics[1] + " topic has been viewed " + sampleFeed.views[1] + " times!"); 

With an output of:

The top topic is Opinion
The Tech topic has been viewed 3 times!

Because we called the method


on three separate lines in the main() method of our class. Every time we call it, it will add 1 view to the total views for the topic “tech”… so if we call it three times it means there are three views of that topic. The value " 1 " inside the parenthesis does not mean " add 1 view "… the value " 1 " is referring to the specific location/part of the array that we are trying to manipulate. We are manipulating the # of views at the first index of the array we created/instantiated/initialized in the main(). The value at the first index starts at zero, and the String at the first index (the subject of the news which is " Tech ") is what the value " 1 " is referring to when we put it inside the parenthesis. The program will work with that index in order to keep track of everything and so that it matches up correctly.


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

Confused as to how the above arrays are automatically fields in the sampleFeed instance.
Explain please.

I believe the next subject 5/8 will answer that for you.

When you get to the next exercise 5/8 I believe it will answer that for you.

Thank you so much this was really helpful this makes perfect sense.