Latest :

Java : Return/Get First Element In Array List | 4 Ways

Java get first element of array list, the following Java program has been written in a multiple ways to print the first element of an array in Java along with outputs, if you have any doubts you can leave a comment here.

Check – Java Return Last Element In Array

This code is for displaying or printing out the first element of a user-defined array.

  • The problem here is to print out the first element in an array whose elements are input by the user.
  • The input here is an array of type int whose elements (i.e. int values) are determined by taking input from the user.
  • Our constraint here is only being able to take input of type int since the array used to store these values can only store int values.
  • The output printed out here is the first element of this int array in the case that the array contains atleast 1 value.

Print the First Element of An Array in Java

Output – 1

  • Solution :
  1. In the first line, a new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java. Scanner class is part of the java.util package and hence the import statement in the beginning is stated to import the functionality of the specified class.
  2.   After that, the user is prompted to enter a number which will determine the size of the array to store elements.
  3. The .nextInt() method is called by the Scanner object sc. This method reads user input of an int value and stores it to the int variable n.
  4. In the case that the user inputs 0 as the array size, the conditional statement checks for the same and prints out a sentence stating the “Array is Empty” and returns or breaks out of the function ending the implementation there itself.
  5. If the input size is not 0, a new int array referenced by variable a is initialized having size n which is the user-defined size.
  6. In the next line, the user is prompted to input n number of elements.
  7. As this loop iterates n number of times, the scanner method calls the .nextInt() method each time to take an int value input from the user and store it at the position in the array.
  8. In the last line of the main method, the first element of array (referenced by a[0]) is printed out.
    Note : The first element in an array is stored at position 0 and not position 1.

Get First Element Of ArrayList

This code is for displaying or printing out the first element of a user-defined array.

  • The problem here is to print out the first element in an array whose elements are input by the user.
  • The input here is an array of type int whose elements (i.e. int values) are determined by taking input from the user.
  • Our constraint here is only being able to take input of type int since the array used to store these values can only store int values.
  • The output printed out here is the first element of this int array in the case that the array contains atleast 1 value.

Output:

  • Solution :
  1. In the first line, a new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java. Scanner class is part of the java.util package and hence the import statement in the beginning is stated to import the functionality of the specified class.
  2.   After that, the user is prompted to enter a number which will determine the size of the array to store elements.
  3. The .nextInt() method is called by the Scanner object sc. This method reads user input of an int value and stores it to the int variable n.
  4. If the input size is not 0, a new int array referenced by variable a is initialized having size n which is the user-defined size.
  5. In the next line, the user is prompted to input n number of elements.
  6. As this loop iterates n number of times, the scanner method calls the .nextInt() method each time to take an int value input from the user and store it at the position in the array.
  7. In the last line of the main method, within the print statement, the method FirstElement is called with array a passed in as an argument. To understand what gets printed out, we shall understand what the method FirstElement does.
  8. It is a static method which means that this method does not belong to any particular instance of the class or an object but it belongs to the class itself. It can be called by the class directly and does not need an object to call it.
  9. This method implements a conditional statement to check is the size of the array is 0. If this condition is satisfied, it prints out a sentence stating the “Array is Empty” and returns a 0.
    Note : The function returns a value 0 rather than not returning anything for array size 0 because the function promises a return type of int.
  10. Otherwise, it returns the first element of the array (referenced by a[0]).
    Note : The first element in an array is stored at position 0 and not position 1.
  11. This returned value is printed out by the print statement in the main method, thus displaying the first element of the array a.

x

Check Also

Java Inverted Mirrored Right Triangle Star Pattern

Java program to print Inverted mirrored right triangle star pattern program. We have written below ...