Skip to content

Instantly share code, notes, and snippets.

@alexejVasko
Created March 10, 2016 13:00
Show Gist options
  • Save alexejVasko/e33b198b40c8e9a8451b to your computer and use it in GitHub Desktop.
Save alexejVasko/e33b198b40c8e9a8451b to your computer and use it in GitHub Desktop.
package array;
public class LastElementOfArray {
public static void main(String[] args) {
// int[] data ={1, 5, 10 , 4};
// System.out.println(getLast(data));
// int[] data ={};
// System.out.println(getLast(data));
// int[] data ={1};
// System.out.println(getLast(data));
int[] data ={0,7};
System.out.println(getLast(data));
}
static int getLast(int[] data){
int result;
if (data.length > 0){
result = data[data.length-1];
}
else{
result=-1;
}
return result;
}
}
@liuiv15
Copy link

liuiv15 commented Mar 10, 2016

А если массив равен null?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment