Login
|
Signup
langref.org
-
perl
,
java
, and
groovy
add..
all
clojure
cpp
csharp
erlang
fantom
fsharp
go
haskell
ocaml
php
python
ruby
scala
Home
All
Solved
Unsolved
Strings
Numbers
Regex
Lists
Maps
Structure
Files
Dates
OOP
Networking
XML
Algorithms
Misc
Parallel
View Problem
Lists
Access
Fetch an element of a list by index
Given the list
[One, Two, Three, Four, Five]
, fetch the third element (
'Three'
)
perl
qw(One Two Three Four Five)[2];
qw(One Two Three Four Five)[2];
perl
@list = qw(One Two Three Four Five);
$list[2];
@list = qw(One Two Three Four Five);
$list[2];
java
String result = list.get(2);
import java.util.List;
import java.util.Arrays;
public class Solution389 {
public static void main(String[] args) {
List<String> list = Arrays.asList(new String[]{"One", "Two", "Three", "Four", "Five"});
String result = list.get(2);
System.out.println(result);
}
}
groovy
1.0+
list = ['One', 'Two', 'Three', 'Four', 'Five']
result = list[2] // index starts at 0
list = ['One', 'Two', 'Three', 'Four', 'Five']
result = list[2] // index starts at 0
assert result == 'Three'
Submit a new solution for
perl
,
java
, or
groovy
There are 21 other solutions in
additional
languages (
clojure
,
cpp
,
csharp
,
erlang
...)