arraylist

第十四单元 集合

自古美人都是妖i 提交于 2020-01-31 02:27:05
一、填空题 1.包的命名规范是 ( 包名全部小写 ). 2 权限访问控制符中,访问权限最小的是 ( private ) 。 3 权限访问控制符中,访问权限最大的是 ( public ) 。 修饰的属性或方法只可以被本类、本包内的类使用,应选择的修饰符是 ( private ) 。 4 面向对象中,继承需要使用到的关键字是 ( extends ) 。 5 、List接口的特点是 ( 有序列表 ) 。 6 、ArrayList和LinkedList的区别是 ( ArrayList底层是数组,LinkedList底层是链表 ) 。 7 、关于ArrayList类的get方法的功能是 ( 通过下标获取元素 ) 。 8 、对ArrayList进行删除元素时,需要的方法是 ( remove() ) 。 9 、如果要获得集合的元素个数,需要调用的方法是 ( size() ) 。 1 0 、List接口的常用实现类包括 ( ArrayList,LinkedList ) 。 11. Collection的常用子接口包括 ( List,Set ) 。 12 Collection的父接口是 ( Iterable ) 。 1 3 基本数据类型int的封装类是 ( Integer ) 。 1 4 、能够去除字符串 首 尾 空格的方法是 ( trim()方法 ) 。 1 5 、能够实现截取字符串操作的方法是

从尾到头打印链表

故事扮演 提交于 2020-01-30 19:44:24
题目描述 输入一个链表,按链表从尾到头的顺序返回一个ArrayList。 栈 1 /** 2 * public class ListNode { 3 * int val; 4 * ListNode next = null; 5 * 6 * ListNode(int val) { 7 * this.val = val; 8 * } 9 * } 10 * 11 */ 12 import java.util.ArrayList; 13 import java.util.*; 14 public class Solution { 15 public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { 16 17 Stack<Integer> s = new Stack<Integer>(); 18 ListNode p = listNode; 19 while (p != null) { 20 s.push(p.val); 21 p = p.next; 22 } 23 ArrayList<Integer> ans = new ArrayList<Integer>(); 24 while (!s.empty()) { 25 ans.add(s.pop()); 26 } 27 return ans; 28 29 }

Vector, ArrayList, Array

感情迁移 提交于 2020-01-30 15:26:21
JAVA新手在使用JAVA的时候大概都会遇到这个问题: JAVA中的Array, ArrayList, Vector, List, LinkedList有什么样的区别?尤其是Vector, ArrayList, Array之间的区别?因为这三个概念从本质上来说都是一样的,都是数组的 数据结构 。 关于这个问题,这里有篇文章: http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html 已经解释得很清楚了,我简单概括一下。 Vector这个类是thread safe的。就是说,多线程同时调用同一个Vector的方法不会造成数据的混乱。而ArrayList不是thread safe。所以,如果是多线程的程序,多使用Vector,这样不需要自己维护concurrency。 数据的增长模式不一样。无论是Vector还是ArrayList,内部都是用一个Array来实现的,这就意味着,一旦数据越来越多超过了原来Array的容量的时候,Vector和ArrayList都需要扩充Array来满足新的数据。Vector每次扩充的时候都增长一倍,就是说新的Array的size是旧的Array的size的2倍。而ArrayList每次扩充,它的Array的size只增加50%. 最后,如果程序对于性能要求很高的话

Copy constructor of Array List

情到浓时终转凉″ 提交于 2020-01-30 13:19:09
问题 I just read about deep copy of ArrayList, that people think new ArrayList<>(originalList); will create a shallow copy. And I wrote a small demo ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris")); List<String> copyNameList1=originalNameList; List<String> copyNameList2= new ArrayList<>(originalNameList); originalNameList.add("Duke"); copyNameList1.add("Ellen"); copyNameList1.set(1,"Bill"); copyNameList2.set(0,"Andy"); System.out.println(

Copy constructor of Array List

蓝咒 提交于 2020-01-30 13:18:23
问题 I just read about deep copy of ArrayList, that people think new ArrayList<>(originalList); will create a shallow copy. And I wrote a small demo ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris")); List<String> copyNameList1=originalNameList; List<String> copyNameList2= new ArrayList<>(originalNameList); originalNameList.add("Duke"); copyNameList1.add("Ellen"); copyNameList1.set(1,"Bill"); copyNameList2.set(0,"Andy"); System.out.println(

Copy constructor of Array List

这一生的挚爱 提交于 2020-01-30 13:17:08
问题 I just read about deep copy of ArrayList, that people think new ArrayList<>(originalList); will create a shallow copy. And I wrote a small demo ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris")); List<String> copyNameList1=originalNameList; List<String> copyNameList2= new ArrayList<>(originalNameList); originalNameList.add("Duke"); copyNameList1.add("Ellen"); copyNameList1.set(1,"Bill"); copyNameList2.set(0,"Andy"); System.out.println(

How can I find if my ArrayList of int[] contains an int[]

妖精的绣舞 提交于 2020-01-30 09:09:06
问题 I have an ArrayList containing int[] . Assuming Arrays.equals(int[], (a value in the arrayList)) = true why when I do arrayOfInts.contains(int[]) is this false? I assume it is because of the same reason that (int[] == (a value in the arrayList)) = false but should I be getting this error? Can I correct it somehow? TL;DR: Can I use Arraylist.contains() for int[] EDIT: ANSWER (if anyone searches this in the future here's the comparison code I made as a solution): private boolean

集合框架

柔情痞子 提交于 2020-01-30 08:51:23
参考网址:http://blog.csdn.net/speedme/article/details/22398395 一.集合 1.集合与数组的区别 总述:几乎有有的集合都是基于数组来实现的, 因为集合是对数组做的封装,所以,数组永远比任何一个集合要快,但任何一个集合,比数组提供的功能要多 (1)数组声明了它容纳的元素的类型,而集合不声明。这是由于集合以object形式来存储它们的元素。 (2)一个数组实例具有固定的大小,不能伸缩。集合则可根据需要动态改变大小。 (3)数组是一种可读/可写数据结构---没有办法创建一个只读数组。然而可以使用集合提供的ReadOnly方法,以只读方式来使用集合。该方法将返回一个集合的只读版本。 集合的特点: a.空间可增长 b.可以存储不同类型的数据 数组的特点: a.空间固定 b.只能存储某种类型的数据 集合的分类: a.collection:List(ArrayList,LinkedList)和Set(HashSet,TreeSet,LinkedHashSet) b.Map(映射):HashMap,TreeMap和LinkedHashMap Collection与Map的区别: 容器内每个为之所存储的元素个数不同。 Collection类型者,每个位置只有一个元素。 Map类型者,持有 key-value pair,像个小型数据库。 注意:

Sort an ArrayList by an object's attribute in java [duplicate]

怎甘沉沦 提交于 2020-01-30 08:40:43
问题 This question already has answers here : Sorting an ArrayList of objects using a custom sorting order (10 answers) Closed 5 months ago . I wish to sort my objects in order of their Email address. This is the method I've attempted but it does not work, but I'm not even sure it's the correct way to do what I want? public static ArrayList<Billing> sortedListByEmail(ArrayList<Billing> Billing) { ArrayList<Billing> Sort = new ArrayList<Billing>(); for (int i = 0; i < Sort.size(); i++) {

Initializing arraylist without object type - JAVA [duplicate]

杀马特。学长 韩版系。学妹 提交于 2020-01-30 08:34:25
问题 This question already has an answer here : Why Diamond operator was not missed from Right hand side in Java 7? (1 answer) Closed 2 years ago . This question is not about why we initialize a list as interface over implementation e.g. List<myObject> obj = new ArrayList<myObject>(); The question is what is the difference between the following two and why do they (apparently) work the same way? //list and arraylist both have a type List<myObject> obj = new ArrayList<myObject>(); //arraylist does