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;

11 Likes

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!

6 Likes
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());
    
    sampleFeed.viewTopic(1);
    sampleFeed.viewTopic(1);
    sampleFeed.viewTopic(3);
    sampleFeed.viewTopic(2);
    sampleFeed.viewTopic(2);
    sampleFeed.viewTopic(1);
    
    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

sampleFeed.viewTopic(1);

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.

8 Likes

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.

1 Like

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

1 Like

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

1 Like

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

For the second step this line also works, to simplify things

views[topicIndex]++;

which is the same as views[topicIndex] = views[topicIndex] + 1;

2 Likes

Let me break line by line:

We have augmented the Newsfeed class to start with the topics array as an instance field.

Fill in the getTopTopic() method to return the 0th index of the topics array.

That means we have to find out what is in the 0th index?

to do that we can return topics[0] then when call the getTopTopic() from main method , we will get The top topic is Opinion

Its easy right?

Ok lets go to the next step and that is little bit confusing. I am sharing what I have figured out.

if someone views an "Opinion" piece, we want to increase the value of the 0th index of views from 0 to 1 . If they view it again, we want to increase the value to 2 .

Confusion 1

In main method when we access viewTopic() method from sampleFeed object by passing argument 1, question is here how many times we are doing this thing? It seems we have done it for 3 times .


sampleFeed.viewTopic(1); // 1

sampleFeed.viewTopic(1); // 2

sampleFeed.viewTopic(3);

sampleFeed.viewTopic(2);

sampleFeed.viewTopic(2);

sampleFeed.viewTopic(1); // 3

Confusion 2:


public void viewTopic(int topicIndex){

views[topicIndex] = views[topicIndex] + 1;

// Inside the views array, it takes int topicIndex right?

We will explain later in at the end.

// Every time someone views a topic, we want to increment the value of the appropriate field in views .

Here, it works like that:


numCats = numCats + 1;


System.out.println("The " + sampleFeed.topics[1] + " topic has been viewed " + sampleFeed.views[1] + " times!");

If you change the sampleFeed.views[1] to sampleFeed.views[2], it will output 2 times. because it is seen two times( look at above). For example: views[0] = views[0] + 1, views[0] + 1 => 2 times.

If you change the rules like you want to show viewers for a single view, you will get 2 times seen then it will be like that:

views[topicIndex] = views[topicIndex] + 2;

My explanation might seem messy but that’s al I have got to help you.