How to get the first and the last elements of array in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        int[] arr = {4, 7, 8, 1, 5};
        
        int first = arr[0];
        int last = arr[arr.length - 1];
        
        System.out.println(last);
    }
}



/*
run:

5

*/

 



answered Mar 17, 2021 by avibootz

Related questions

1 answer 156 views
1 answer 158 views
1 answer 169 views
2 answers 158 views
1 answer 129 views
...