-8

here is my code:

import java.util.Random;

public class Lehma {

    private static final String[] NAMES = new String[]{
    "Anu", "Arpa", "Essi", "Heluna", "Hely",
    "Hento", "Hilke", "Hilsu", "Hymy", "Matti", "Ilme", "Ilo",
    "Jaana", "Jami", "Jatta", "Laku", "Liekki",
    "Mainikki", "Mella", "Mimmi", "Naatti",
    "Nina", "Nyytti", "Papu", "Pullukka", "Pulu",
    "Rima", "Soma", "Sylkki", "Valpu", "Virpi"};

    private String name;
    private int volumes;


    public Lehma() {
        int index = new Random().nextInt(NAMES.length);
        // Get index item from NAMES list                     
    }     
}

How I can get specific index from NAMES string array? I tried to use some get methods, but it didn't work. For example if index is 1, then the return should be "Arpa" because Arpa's index is 1 in NAMES array.

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
Juho M
  • 349
  • 2
  • 9
  • 19

2 Answers2

2

Just use this syntax:-

If you wanna need value from index 0 then NAMES[0]

If you wanna need value from index 1 then NAMES[1]

If you wanna need value from index 2 then NAMES[2]

and so on..

letsintegreat
  • 3,328
  • 4
  • 18
  • 39
0

Try:

public String getItem(String[] arr, int index) {
     return arr[index];
}