62

If I have an array of doubles:

[10.2, 20, 11.1, 21, 31, 12, 22.5, 32, 42, 13.6, 23, 32, 43.3, 53, 14, 24, 34, 44, 54, 64, 15.1, 25, 35, 45, 55, 65.3, 75.4, 16, 26, 17.5,]

and I want to get the first element and last element so that

firstNum = 10.2

lastNum = 17.5

how would I do this?

Jotaro
  • 707
  • 2
  • 6
  • 7
  • 3
    The problem with most of the answers using the .length to get array size is that you have to create a variable for the list to get its size. Check https://stackoverflow.com/questions/21426843/get-last-element-of-stream-list-in-a-one-liner – devssh Apr 02 '18 at 15:24
  • Thats right you might have declared an array to hold 25 items, But you may have filled only 10. so (array.length-1) will still give you the index of the 25th item, not the last item you filled in i.e actually in the 10th position or 9th index! – Yo Apps Oct 21 '19 at 10:32

7 Answers7

114

If you have a double array named numbers, you can use:

firstNum = numbers[0];
lastNum = numbers[numbers.length-1];

or with ArrayList

firstNum = numbers.get(0);
lastNum = numbers.get(numbers.size() - 1); 
Steffen Harbich
  • 2,639
  • 2
  • 37
  • 71
K Richardson
  • 1,640
  • 1
  • 10
  • 14
  • 3
    Adding a documentation reference to make the answer more complete: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – acoelhosantos Jul 19 '17 at 09:41
9

In Java, you can get the first and last elements of an array using the following approaches:

1. Using Array Indexing:

To get the first element, you can access it using index 0. To get the last element, you can access it using the index array.length - 1. Here's an example:

int[] array = {1, 2, 3, 4, 5};

int firstElement = array[0];
int lastElement = array[array.length - 1];

System.out.println("First Element: " + firstElement);
System.out.println("Last Element: " + lastElement);

2. Using ArrayList (if the array is converted to ArrayList):

If you have converted the array to an ArrayList, you can use the get() method to access the first and last elements. To get the first element, use arrayList.get(0). To get the last element, use arrayList.get(arrayList.size() - 1). Here's an example:

import java.util.ArrayList;

int[] array = {1, 2, 3, 4, 5};
ArrayList<Integer> arrayList = new ArrayList<>();

// Convert the array to ArrayList
for (int i : array) {
    arrayList.add(i);
}

int firstElement = arrayList.get(0);
int lastElement = arrayList.get(arrayList.size() - 1);

System.out.println("First Element: " + firstElement);
System.out.println("Last Element: " + lastElement);

Both approaches allow you to retrieve the first and last elements of an array in Java. Choose the one that best fits your needs and the type of data structure you are working with.

Yagmur SAHIN
  • 277
  • 3
  • 3
  • 9
    Please put your answer in some context and do not just paset code. See [here](https://stackoverflow.com/help/how-to-answer) for further info – gehbiszumeis Jan 21 '19 at 08:05
5
// Array of doubles
double[] array_doubles = {2.5, 6.2, 8.2, 4846.354, 9.6};

// First position
double firstNum = array_doubles[0]; // 2.5

// Last position
double lastNum = array_doubles[array_doubles.length - 1]; // 9.6

This is the same in any array.

3

Check this

double[] myarray = ...;
System.out.println(myarray[myarray.length-1]); //last
System.out.println(myarray[0]); //first
fsalazar_sch
  • 348
  • 2
  • 6
  • 17
2

I think there is only one intuitive solution and it is:

int[] someArray = {1,2,3,4,5};
int first = someArray[0];
int last = someArray[someArray.length - 1];
System.out.println("First: " + first + "\n" + "Last: " + last);

Output:

First: 1
Last: 5
Alexander Grass
  • 384
  • 2
  • 11
2

getting first and last element in an array in java

double[] numbers = {10.2, 20, 11.1, 21, 31, 12, 
22.5, 32, 42, 13.6, 23, 32, 43.3, 53, 14, 24, 34,
44, 54, 64, 15.1, 25, 35, 45, 55, 65.3, 75.4, 16, 26, 17.5,};


double firstNumber= numbers[0];

double lastNumber = numbers[numbers.length-1];

System.out.println("firstNum = "+ firstNumber);

System.out.println("lastNum = "+ lastNumber);
1

This is the given array.

    int myIntegerNumbers[] = {1,2,3,4,5,6,7,8,9,10};

// If you want print the last element in the array.

    int lastNumerOfArray= myIntegerNumbers[9];
    Log.i("MyTag", lastNumerOfArray + "");

// If you want to print the number of element in the array.

    Log.i("MyTag", "The number of elements inside" +
            "the array " +myIntegerNumbers.length);

// Second method to print the last element inside the array.

    Log.i("MyTag", "The last elements inside " +
            "the array " + myIntegerNumbers[myIntegerNumbers.length-1]);