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

 

srp4379@lausd.net 1-arraybasics > getLastElement2
prev  |  next  |  chance

public int getLastElement2(int[] nums)

All arrays will have 4 elements.

If the arrays you're testing all have the same length,
you don't have to use the expression [len-1].
You can just use the position number itself.

For example:
If the list has 4 elements, use nums[3] for int[] nums.
If the list has 10 elements, use words[9] for String[] words.
If the list has 3 elements, use list[2] for int[] list.

So if the list has 4 elements, 3 is the position of the last element.

public String getLastElement2(String[] words) {
  String last = words[3];
  return last;
}

If, however, the list can be ANY size, use an expression.
public String getLastElement2(String[] words) {
  int len = words.length;
  String last = words[len-1];
  return last;
}

getLastElement2(["red", "green", "blue", "gold"]) → "gold"
getLastElement2(["a", "b", "c", "d"]) → "d"
getLastElement2(["w", "x", "y", "z"]) → "z"

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

public String getLastElement2(String[] words) { }

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