id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

srp4379@lausd.net > getFirstElement1
prev  |  next  |  chance

public int getFirstElement1(int[] list)

Given an array of integers values, return the value of the first element

An array / list is a collection of elements, all of the same type.
int[] list = {1,2,3} creates a list of ints (integers).
String[] list = {"To","be","or","not","to","be"} creates a list of Strings.

The way you access an individual element of a list is by its index / position.
The syntax is:
(a) the name of the list variable followed by
(b) the index number inside of square brackets.
For example:

nums[0] is the 1st element of the array variable int[] nums.
list[1] is the 2nd element of the array variable int[] list.
words[2] is the 3rd element of the array variable String[] words.

As with Strings, you can use expressions instead of hard-coded numbers.
For example:

int[] values;
int len = values.length; // NOTE: no parentheses after length
values[len-1] is the last element in values
values[len-2] is the 2nd-to-last element in values

public int getFirstElement(int[] list) {
  int first = list[0];
  return first;
}

getFirstElement1([1, 2, 3, 4, 5]) → 1
getFirstElement1([5, 10, 15, 20]) → 5
getFirstElement1([3, 6, 9]) → 3

...Save, Compile, Run (ctrl-enter)

public int getFirstElement1(int[] list) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 1

Copyright Nick Parlante 2017 - privacy