Adam Short
Adam Short

Reputation: 498

Get ArrayList index

I'm trying to access the current index of an array list but can't figure out how to do it in the following method:

public String getNextMessage(){
    String s = getMessageArray(index);
    index++;
    return s;
}

The "getMessageArray" comes up with an error and I don't know hwo to resolve it. This method worked with Arrays[] but I don't know how to do it with an ArrayList. Here is the whole class file:

package com.game.main;

import java.util.ArrayList;

public class Message {

private int index;
private ArrayList<String> messageArray;

private String intro = "Welcome.";

public Message() {
    messageArray = new ArrayList<String>();
    messageArray.add(intro);
}

public String getNextMessage(){
    String s = getMessageArray(index);
    index++;
    return s;
}

public ArrayList<String> getMessageArray() {
    return messageArray;
}

public void setMessageArray(ArrayList<String> messageArray) {
    this.messageArray = messageArray;
}
}

Upvotes: 1

Views: 7270

Answers (3)

Mohammed Falha
Mohammed Falha

Reputation: 967

Instead of

getMessageArray(index); 

write

getMessageArray.get(index);  

you must first get the array list then specify the index you want to get.

Upvotes: 0

Zavior
Zavior

Reputation: 6452

As I wrote in an earlier answer

public String getNextMessage(){
    String s = messages.get(indx);
    indx++;
    return s;
}

however, it seems you changed the part where the list is accessed from

String s = messages.get(indx);

to

String s = getMessageArray(index);

The reason your version doesnt work is, that the you are trying to call a method called getMessageArray(index), that returns the ArrayList object itself, which is an ArrayList. What you actually want to do is to call the get()-method of the ArrayList class.

You have declared a variable called messages earlier in your class

private ArrayList<String> messages;

Here you can see messages is of type ArrayList. ArrayList has a method called get(int index) which retrieves the element at the specified index.

You can call this method the way I wrote at the beginning of this answer.

For reference, you can view all methods available on arraylist here.

Upvotes: 1

Rohit Jain
Rohit Jain

Reputation: 213243

I guess your first line in getNextMessage() should be:

String s = getMessageArray().get(index);

There is no such method as: getMessageArray(int) in your class. It's the method with 0-arg, which returns an ArrayList, on which you use the List#get(int) method to access the value at some index.

In fact, you can simply access the messageArray directly in that method, as it's in the same class only. So, that line can be better written as:

String s = messageArray.get(index);

Now, given that you are not doing anything between fetching the value at that index, and returning that value, apart from incrementing the index, you can shorten your method to a one-liner:

public String getNextMessage(){
    return messageArray.get(index++);
}

Upvotes: 1

Related Questions