- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包额外内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
Java LinkedList addLast() 方法
描述
Java LinkedList addLast(E e) 方法将指定的元素 E 插入到 linkedList 的末尾。它保持插入顺序。当我们多次调用此方法时,元素会不断添加到列表的末尾。
声明
以下是 java.util.LinkedList.addLast() 方法的声明
public void addLast(E e)
参数
e − 要添加到末尾的元素。
返回值
此方法不返回任何值。
异常
NullPointerException − 如果指定的元素为 null。
向整数 LinkedList 的末尾添加元素示例
以下示例演示了使用整数的 Java LinkedList addLast(E) 方法的使用。我们使用 add() 方法向列表中添加一些元素,然后使用 addLast() 方法在末尾添加两个元素。最后,我们打印 LinkedList 对象以查看最终结果。
package com.tutorialspoint;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
// create an empty linkedList
LinkedList<Integer> linkedList = new LinkedList<>();
// use add() method to add elements in the linkedList
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.add(4);
linkedList.add(5);
linkedList.add(6);
// the values will be printed in the same order
linkedList.addLast(7);
linkedList.addLast(8);
// let us print all the elements available in linkedList
System.out.println("LinkedList = " + linkedList);
}
}
输出
让我们编译并运行上面的程序,这将产生以下结果:
LinkedList = [1, 2, 3, 4, 5, 6, 7, 8]
向字符串 LinkedList 的末尾添加元素示例
以下示例演示了使用字符串的 Java LinkedList addLast(E) 方法的使用。我们使用 add() 方法向列表中添加一些元素,然后使用 addLast() 方法在末尾添加两个元素。最后,我们打印 LinkedList 对象以查看最终结果。
package com.tutorialspoint;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
// create an empty linkedList
LinkedList<String> linkedList = new LinkedList<>();
// use add() method to add elements in the linkedList
linkedList.add("A");
linkedList.add("B");
linkedList.add("C");
linkedList.add("D");
linkedList.add("E");
linkedList.add("F");
// the values will be printed in the same order
linkedList.addLast("G");
linkedList.addLast("H");
// let us print all the elements available in linkedList
System.out.println("LinkedList = " + linkedList);
}
}
输出
让我们编译并运行上面的程序,这将产生以下结果:
LinkedList = [A, B, C, D, E, F, G, H]
向对象 LinkedList 的末尾添加元素示例
以下示例演示了使用 Student 对象的 Java LinkedList addLast(E) 方法的使用。我们使用 add() 方法向列表中添加一些元素,然后使用 addLast() 方法在末尾添加两个元素。最后,我们打印 LinkedList 对象以查看最终结果。
package com.tutorialspoint;
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
// create an empty linkedList
LinkedList<Student> linkedList = new LinkedList<>();
// use add() method to add elements in the linkedList
linkedList.add(new Student(1, "Julie"));
linkedList.add(new Student(2, "Robert"));
linkedList.add(new Student(3, "Adam"));
// use addLast() method to add element at the end of the linkedList
linkedList.addLast(new Student(4, "Rohan"));
linkedList.addLast(new Student(5, "Sohan"));
// let us print all the elements available in linkedList
System.out.println("LinkedList = " + linkedList);
}
}
class Student {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
}
输出
让我们编译并运行上面的程序,这将产生以下结果:
LinkedList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Rohan ], [ 5, Sohan ]]
java_util_linkedlist.htm
广告