-2

In python, calling the value of a list by only referring to its index number is done by

array = [1, 2, 3, 4, 5,]
return array[0]

returns

1

Is there a similar way to do this in java? I have only been able to find calling values of an array and getting their index using indexOf().

ArrayList<String> wordList = new ArrayList<String>();
wordList.add("desk");
wordList.add("monitor");

return wordList[1];

Returns error

array required, but ArrayList found return wordList[1]; ^

I would want it to return the value that is in index 1, which is 'monitor'

0

4 Answers 4

3

Yes. By using an array.

int[] array = {1, 2, 3, 4, 5};
return array[0]; // for 1, for 2 use array[1], etc.

You can also do it with a List. But there you need to call List.get(int) instead of [int]. Like,

List<String> wordList = List.of("desk", "monitor");
return wordList.get(1); // "monitor"

Or with a String[] just like an int[]

String[] wordArray = { "desk", "monitor" };
return wordArray[1]; // "monitor"
Sign up to request clarification or add additional context in comments.

Comments

1

It is because your wordList is an Object of type ArrayList (ArrayList Object) and NOT an array.

Try creating an array of type String:

String[] wordList = {"array", "items", "here"};
return wordList[2] //Would equal "here"

Comments

1

"... Is there a similar way to do this in java? I have only been able to find calling values of an array and getting their index using indexOf(). ..."

Not for a List.  In Java, the bracket syntax is used exclusively for array notation.

Comments

1

You're confusing Java array with ArrayList. An ArrayList is a type of Collection backed by an array.

I don't know Python, so I'll address this only from a Java point of view.

// foo is an array of int, and contains 12 elements
int [] foo = new foo [12]; 

// bar is an array of int, with 7 elements. 
// the initial values are specified
int [] bar = {14, 92, -42, 48, 0, 0, 13};

An element is addressed as you expect:
System.out.println (bar [2]); // prints -42

If you want to fix your example using an array, the code might look like this:

String [] wordList = {"desk", "monitor"};
System.out.println (wordList[1]); // monitor

Collections are more flexible than arrays. One consideration is that an array has a fixed size, while most Collection Objects will increase their sizes as needed. Suppose an array is used, and it is discovered it is too small. To continue, a new array would have to be created, the contents of the old array would be copied into the new array, and the old array discarded.

If you wanted to fix your Java example to use an ArrayList, it might look like this, with a call to get:

ArrayList<String> wordList = new ArrayList<String>();
wordList.add("desk");
wordList.add("monitor");

return wordList.get(1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.