arraylist

List list = new ArrayList();和ArrayList list=new ArrayList();的区别

依然范特西╮ 提交于 2019-12-22 21:34:57
List是一个接口,而ArrayList 是一个类。 ArrayList 继承并实现了List。 List list = new ArrayList();这句创建了一个ArrayList的对象后把上溯到了List。此时它是一个List对象了,有些ArrayList有但是List没有的属性和方法,它就不能再用了。而ArrayList list=new ArrayList();创建一对象则保留了ArrayList的所有属性。 为什么一般都使用 List list = new ArrayList() ,而不用 ArrayList alist = new ArrayList()呢? 问题就在于List有多个实现类,如 LinkedList或者Vector等等,现在你用的是ArrayList,也许哪一天你需要换成其它的实现类呢?,这时你只要改变这一行就行了:List list = new LinkedList(); 其它使用了list地方的代码根本不需要改动。假设你开始用 ArrayList alist = new ArrayList(), 这下你有的改了,特别是如果你使用了 ArrayList特有的方法和属性。 ,如果没有特别需求的话,最好使用List list = new LinkedList(); ,便于程序代码的重构. 这就是面向接口编程的好处 来源: https://www

Undo changes in an arrayList

落花浮王杯 提交于 2019-12-22 20:14:20
问题 I've an ArrayList of Line Objects called 'lines'. I made my own line class to draw lines with some constraints. It involves selecting two points in a panel and a line is drawn connecting the two points. Everytime a line is created, it is added to the 'lines'. The lines are drawn in a panel. The paint function in my panel looks like this: public void paintComponent(Graphics g){ super.paintComponent(g); for(final Line r:lines){ r.paint((Graphics2D)g); } } And everytime two points are clicked on

Undo changes in an arrayList

安稳与你 提交于 2019-12-22 20:14:09
问题 I've an ArrayList of Line Objects called 'lines'. I made my own line class to draw lines with some constraints. It involves selecting two points in a panel and a line is drawn connecting the two points. Everytime a line is created, it is added to the 'lines'. The lines are drawn in a panel. The paint function in my panel looks like this: public void paintComponent(Graphics g){ super.paintComponent(g); for(final Line r:lines){ r.paint((Graphics2D)g); } } And everytime two points are clicked on

How to serialize ArrayList of objects?

别等时光非礼了梦想. 提交于 2019-12-22 19:42:48
问题 I want to serialize an arraylist of Item but it doesn't work.... my Item class extends Stuff class and has some subclasses . all of my classes implement Serilalizable. i have this part : try{ // Serialize data object to a file ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("MyData.ser")); out.writeObject(myData); out.close(); // Serialize data object to a byte array ByteArrayOutputStream bos = new ByteArrayOutputStream() ; out = new ObjectOutputStream(bos) ; out

How to serialize ArrayList of objects?

时光毁灭记忆、已成空白 提交于 2019-12-22 19:42:28
问题 I want to serialize an arraylist of Item but it doesn't work.... my Item class extends Stuff class and has some subclasses . all of my classes implement Serilalizable. i have this part : try{ // Serialize data object to a file ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("MyData.ser")); out.writeObject(myData); out.close(); // Serialize data object to a byte array ByteArrayOutputStream bos = new ByteArrayOutputStream() ; out = new ObjectOutputStream(bos) ; out

java面试题2020

筅森魡賤 提交于 2019-12-22 17:52:51
1:String类 能被继承吗?为什么? 不能,因为String类有final修饰符,final类无法被继承。 2:ArrayList和LinkedList有什么区别? a:ArrayList 底层是数组,而LinkedList底层是双向链表结构。 b:都是不同步的,不保证线程安全 c:插入和删除是否受元素位置的影响:ArrayList采用数组存储,所以插入和删除元素的时间受数组的复杂度影响,默认插入是在最后一位 时间复杂度是O(1),如果指定位置插入(add(int index,E element)时间复杂度就是(O(n-i)。因为在执行的时候集合中第i和第i个元素之后的(n-i)都要执行向前或向后一位的操作 LinkedList采用链表存储,所以插入,删除不受时间复杂度影响 都是近似O(1) d:是否支持快速随机访问:LinkedList 不支持高校的随机元素访问,而ArrayList支持。快速随机访问就是通过元素的序号快速获取元素对象(对应于get(index)方法。 e. 内存空间占用: ArrayList的空 间浪费主要体现在在list列表的结尾会预留一定的容量空间,而LinkedList的空间花费则体现在它的每一个元素都需要消耗比ArrayList更多的空间(因为要存放直接后继和直接前驱以及数据) 3:g1和cms区别,吞吐量优先和响应优先的垃圾收集器选择。

Most efficient way to build a random permutations list

谁都会走 提交于 2019-12-22 17:52:22
问题 For a given Collection<Object> aCollection , How do I build an ArrayList<OrderedCouple<Object>> with all possible permutations of couples in aCollection (except self-coupling). For instance, say aCollection is a Set<Team> containing teamA , teamB and teamC , and OrderedCouple is instead a class Game<Team> which constructor receives two team, the host and the guest as arguments. I want to build an ArrayList of all possible Game s between Team s. that is, the ArrayList will be the group {new

How to update an item from ArrayList on ArrayAdapter in Android

大兔子大兔子 提交于 2019-12-22 17:22:21
问题 I am new at developing Android.I have a ListView and custom ArrayAdapter which holds an arraylist.I want to update arraylist specific item by position.How can I handle please help me thanks in advance. 回答1: Assuming your adapter is storing a List of Foo objects. Foo item = mAdapter.getItem(position); item.setValue("blah"); //Or whatever you need to update mAdapter.notifyDataSetChanged(); 来源: https://stackoverflow.com/questions/27344840/how-to-update-an-item-from-arraylist-on-arrayadapter-in

Sort Android apps alphabetically?

ⅰ亾dé卋堺 提交于 2019-12-22 13:53:47
问题 I have the following code: packageManager = getPackageManager(); List<PackageInfo> packageList = packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS); List<PackageInfo> installedapps = new ArrayList<PackageInfo>(); for(PackageInfo apps: packageList){ if(!isSystemPackage(apps)){ installedapps.add(apps); } } Collections.sort(installedapps, new Comparator<PackageInfo>(){ public int compare(PackageInfo o1, PackageInfo o2) { return o1.packageName.compareTo(o2.packageName); } });

Java数据结构之LinkedList、ArrayList的效率分析

為{幸葍}努か 提交于 2019-12-22 13:05:45
前言: 在我们平常开发中难免会用到List集合来存储数据,一般都会选择ArrayList和LinkedList,以前只是大致知道ArrayList查询效率高LinkedList插入删除效率高,今天来实测一下。 先了解一下List List列表类,顺序存储任何对象(顺序不变),可重复。 List是继承于Collection的接口,不能实例化。实例化可以用: ArrayList(实现动态数组),查询快(随意访问或顺序访问),增删慢。整体清空快,线程不同步(非线程安全)。数组长度是可变的百分之五十延长 LinkedList(实现链表),查询慢,增删快。 Vector(实现动态数组),都慢,被ArrayList替代。长度任意延长。线程安全(同步的类,函数都是synchronized) Stack(实现堆栈)继承于Vector,先进后出。 L ist基本操作 插入:add() 查找:get() 删除:remove(int index) 修改:set() 清空表:clear() 遍历:用Iterator迭代器遍历每个元素 ArrayList、LinkedList性能对比 为了很好的对比效率,直接写个测试程序看下运行结果 模拟5w条数据指定插入第一位,然后查询全部,循环删除第一位,下面是测试ArrayList函数 private void testArrayList(){ ArrayList