- 从List中获取第一个元素:
Listlist = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); String firstElement = list.get(0); System.out.println("First element: " + firstElement);
- 从数组中获取第一个元素:
String[] array = {"A", "B", "C"}; String firstElement = array[0]; System.out.println("First element: " + firstElement);
- 使用Iterator获取第一个元素:
Listlist = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); Iterator iterator = list.iterator(); String firstElement = iterator.next(); System.out.println("First element: " + firstElement);