51

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
  • 587
  • 1
  • 5
  • 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

101

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,379
  • 1
  • 32
  • 64
K Richardson
  • 1,500
  • 1
  • 9
  • 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
8

Getting first and last elements in an array in Java

int[] a = new int[]{1, 8, 5, 9, 4};

First Element: a[0]

Last Element: a[a.length-1]
Yagmur SAHIN
  • 197
  • 2
  • 3
  • 8
    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
  • 331
  • 1
  • 5
  • 15
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
  • 314
  • 2
  • 10
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]);
1

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);