banner



How To Get A List Of All Elements In A Ios App Using Eclipse

Technical Questions and Answers

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

How to get first and last elements from ArrayList in Java?


The get() method of the ArrayList class accepts an integer representing the index value and, returns the element of the current ArrayList object at the specified index.

Therefore, if you pass 0 to this method you can get the first element of the current ArrayList and, if you pass list.size()-1 you can get the last element.

Example

 Live Demo

import java.util.ArrayList; public class FirstandLastElemets{    public static void main(String[] args){       ArrayList<String> list = new ArrayList<String>();       //Instantiating an ArrayList object       list.add("JavaFX");       list.add("Java");       list.add("WebGL");       list.add("OpenCV");       list.add("OpenNLP");       list.add("JOGL");       list.add("Hadoop");       list.add("HBase");       list.add("Flume");       list.add("Mahout");       list.add("Impala");       System.out.println("Contents of the Array List: \n"+list);       //Removing the sub list       System.out.println("First element of the array list: "+list.get(0));       System.out.println("Last element of the array list: "+list.get(list.size()-1));    } }

Output

Contents of the Array List: [JavaFX, Java, WebGL, OpenCV, OpenNLP, JOGL, Hadoop, HBase, Flume, Mahout, Impala] First element of the array list: JavaFX Last element of the array list: Impala

Example 2

To get minimum and maximum values of an ArrayList −

  • Create an ArrayList object.

  • Add elements to it.

  • Sort it using the sort() method of the Collections class.

  • Then, the first element of the ArrayList will be the minimum value and the last element of the ArrayList will be the maximum value.

 Live Demo

import java.util.ArrayList; import java.util.Collections; public class MinandMax{    public static void main(String[] args){       ArrayList<Integer> list = new ArrayList<Integer>();       //Instantiating an ArrayList object       list.add(1001);       list.add(2015);       list.add(4566);       list.add(90012);       list.add(100);       list.add(21);       list.add(43);       list.add(2345);       list.add(785);       list.add(6665);       list.add(6435);       System.out.println("Contents of the Array List: \n"+list);       //Sorting the array list       Collections.sort(list);       System.out.println("Minimum value: "+list.get(0));       System.out.println("Maximum value: "+list.get(list.size()-1));    } }

Output

Contents of the Array List: [1001, 2015, 4566, 90012, 100, 21, 43, 2345, 785, 6665, 6435] Minimum value: 21 Maximum value: 90012

raja

Published on 11-Oct-2019 07:21:09

  • Related Questions & Answers
  • Get first and last elements from Java LinkedList
  • Get first and last elements from Vector in Java
  • Get first and last elements of a list in Python
  • Get all rows apart from first and last in MySQL
  • Delete first and last element from a LinkedList in Java
  • Get Synchronized List from ArrayList in java
  • Remove all elements from the ArrayList in Java
  • How to change first and last elements of a list using jQuery?
  • Get last N elements from given list in Python
  • Python program to interchange first and last elements in a list
  • How to remove the redundant elements from an ArrayList object in java?
  • Get last key from NavigableMap in Java
  • Get last entry from NavigableMap in Java
  • How to get the first and last record of the table in MySQL?
  • Get first key from NavigableMap in Java

How To Get A List Of All Elements In A Ios App Using Eclipse

Source: https://www.tutorialspoint.com/how-to-get-first-and-last-elements-from-arraylist-in-java

Posted by: tarnowskilovetted.blogspot.com

0 Response to "How To Get A List Of All Elements In A Ios App Using Eclipse"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel