Created
March 10, 2016 13:00
-
-
Save alexejVasko/e33b198b40c8e9a8451b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
А если массив равен null?