找到关于面向对象编程的9301 篇文章
114 次查看
java.util.LinkedList 类的 getFirst() 方法返回当前列表的第一个元素。示例 实时演示 import java.util.*; public class LinkedListDemo { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add("Hello"); list.add(2); list.add("Chocolate"); list.add("10"); System.out.println("LinkedList:" + list); System.out.println("First Element :" + list.getFirst()); } } 输出 LinkedList:[Hello, 2, Chocolate, 10] First Element :Hello
141 次查看
java.util.LinkedList 类的 addLast(E e) 方法将指定的元素插入到此列表的末尾。示例:import java.util.*; public class LinkedListDemo { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add("Hello"); list.add(2); list.add("Chocolate"); list.add("10"); System.out.println("LinkedList:" + list); list.addLast("Element"); System.out.println("LinkedList:" + list); } }输出:LinkedList:[Hello, 2, Chocolate, 10] LinkedList:[Hello, 2, Chocolate, 10, Element]
155 次查看
java.util.LinkedList 类的 addFirst(E e) 方法将指定的元素插入到此列表的开头。示例:public class LinkedListDemo { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add("Hello"); list.add(2); list.add("Chocolate"); list.add("10"); System.out.println("LinkedList:" + list); list.addFirst("Element"); System.out.println("LinkedList:" + list); } }输出:LinkedList:[Hello, 2, Chocolate, 10] LinkedList:[Element, Hello, 2, Chocolate, 10]
273 次查看
java.util.ArrayList 类的 add(int index, E element) 方法在此列表的指定位置插入指定的元素 E。它会将当前位于该位置的元素(如果存在)以及任何后续元素向右移动(其索引将加一)。示例 import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add(15); arrlist.add(22); arrlist.add(30); arrlist.add(40); arrlist.add(2,25); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }输出Number = 15 Number = 22 Number = 25 Number = 30 Number = 40
326 次查看
java.util.ArrayList 类的 add(E e) 方法将指定的元素 E 附加到列表的末尾。示例:import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add(15); arrlist.add(20); arrlist.add(25); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }输出:Number = 15 Number = 20 Number = 25
82 次查看
ArrayList 类的 removeRange() 方法从此列表中移除所有索引位于 fromIndex 和 toIndex 之间的元素。示例 import java.util.*; public class ArrayListDemo extends ArrayList{ public static void main(String[] args) { ArrayListDemo arrlist = new ArrayListDemo(); arrlist.add(10); arrlist.add(12); arrlist.add(31); System.out.println("The list:" + arrlist); arrlist.removeRange(0,2); System.out.println("The list after using removeRange:" + arrlist); } }输出The list:[10, 12, 31] The list after using removeRange:[31]
99 次查看
ArrayList 类的 set() 方法用指定的元素替换此列表中指定位置处的元素。示例 import java.util.ArrayList; public class Sample { public static void main(String args[]) { ArrayList al = new ArrayList(); System.out.println("Initial size of al: " + al.size()); al.add("C"); al.add("A"); al.add("E"); al.add(1, "A2"); System.out.println("Size of al after additions: " + al.size()); System.out.println("Contents of al: " + al); System.out.println("Size of al after deletions: " + al.size()); ... 阅读更多
62 次查看
java.util.LinkedList 类的 listIterator(int index) 方法返回此列表中元素的列表迭代器(按正确的顺序),从列表中的指定位置开始。示例 import java.util.*; public class LinkedListDemo { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add("Hello"); list.add(2); list.add("Chocolate"); list.add("10"); System.out.println("LinkedList:" + list); Iterator x = list.listIterator(1); while (x.hasNext()) { System.out.println(x.next()); } } } 输出 LinkedList:[Hello, 2, Chocolate, 10] 2 Chocolate 10
114 次查看
java.util.ArrayList 类的 trimToSize() 方法将此 ArrayList 实例的容量调整为列表的当前大小。应用程序可以使用此操作来最小化 ArrayList 实例的存储空间。示例 import java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { ArrayList arrlist = new ArrayList(5); arrlist.add(35); arrlist.add(20); arrlist.add(25); arrlist.trimToSize(); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }输出Number = 35 Number = 20 Number = 25
844 次查看
由于 Vector 以对象的形式存储元素,因此您可以在其中存储各种类型的对象(异构)。示例:import java.util.*; class Demo{} public class VectorSample { public static void main(String args[]) { Demo obj = new Demo(); Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new String("krishna")); v.addElement(new Float(3.5f)); v.addElement(obj); System.out.println("Capacity after four additions: " + v.capacity()); } }