arraylist

二叉树中和为某一值的路径

拈花ヽ惹草 提交于 2019-12-21 17:27:13
题目描述 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前) import java.util.ArrayList; /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { private ArrayList<ArrayList<Integer>> listAll = new ArrayList<>(); private ArrayList<Integer> list = new ArrayList<>(); public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) { if(root == null) return listAll; list.add(root.val); target -= root.val; if(target == 0 && root.left == null &&

Two-Dimensional ArrayList set an element

﹥>﹥吖頭↗ 提交于 2019-12-21 17:00:31
问题 I have this piece of code: List<ArrayList<Integer>> tree = new ArrayList<ArrayList<Integer>>(); tree.add(0, new ArrayList<Integer>(Arrays.asList(1))); tree.add(1, new ArrayList<Integer>(Arrays.asList(2,3))); tree.add(2, new ArrayList<Integer>(Arrays.asList(4,5,6))); I would like, for example, to replace 5 with 9. how can I do this? 回答1: use tree.get(2).set(1,Integer.valueOf(9)); to get the ArrayList at the position 2 and then set the second element to 9. 回答2: tree.get(2).set(1, 9) This gets

Problems with two threads accessing a frequently updated Arraylist

白昼怎懂夜的黑 提交于 2019-12-21 16:53:13
问题 I have ArrayLists that store many objects, and objects are frequently added and removed from the ArrayLists. One thread operates on the data structures and updates the ArrayList's objects every 20ms or so. Another thread traverses the ArrayLists and uses their elements to paint the objects (also every 20-30ms). If the ArrayLists are traversed using a for loop, IndexOutOfBoundsExceptions abound. If the ArrayLists are traversed using iterators, ConcurrentModificationExceptions abound.

LeetCode DFS 78 Subsets 90 Subsets ll

别说谁变了你拦得住时间么 提交于 2019-12-21 14:41:26
Subsets Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: nums = [1,2,3] Output: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] DFS (backtracking) 详情可以参考九章算法基础 lecture 1 class Solution { public List < List < Integer > > subsets ( int [ ] nums ) { List < List < Integer > > result = new ArrayList < > ( ) ; //ArrayList<ArrayList<Integer>> result = new ArrayList<>(); if ( nums == null ) { return result ; } if ( nums . length == 0 ) { result . add ( new ArrayList < Integer > ( ) ) ;

Java面试题总结---01

北慕城南 提交于 2019-12-21 12:25:27
# 常见的Java问题 ## 什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? - Java虚拟机是一个可以执行Java字节码文件的虚拟机进程。Java源文件被编译成能够被Java虚拟机执行的字节码文件。 - Java是被设计为允许应用程序在任意平台都可以允许,不需要我们为每一个平台单独重写或者重新编译,Java虚拟机使其变成可能,因为它直到底层硬件平台的指令长度和其他特性。 ## JDK和JRE的区别是什么? - JRE是将要执行Java程序的Java虚拟机,它同时包含了执行applet需要的浏览器插件。 - JDK是完整的Java程序开发包,包含了JRE、编译器和其他工具。 所以,我们通常情况下,只需要下载JDK、配置好开发环境就可以了 ## "static"关键字是什么意思?Java能否覆盖(override)一个private或则static的方法 - “static”关键字表名一个成员变量或者一个成员方法可以在没有所属类的实例变量的情况下被访问,也就是我们不用使用new关键字构建一个对象,使用**类名.成员变量**或者**类名.成员方法**就可以。 - Java中的static方法不能被覆盖,因为方法覆盖是基于动态绑定的,而static方法使编译时静态绑定的。(换句话说,一个static方法,或者static属性

Java: ArrayList add() and remove() performance, implementation?

萝らか妹 提交于 2019-12-21 12:07:38
问题 I have read somewhere that ArrayList's add() and remove() operations run in "amortized constant" time. What does this mean exactly? In the implementation of add(item) I can see that it ArrayList uses an array buffer, which is at most 3/2 of the list't size, and if it is full, System.arraycopy() is called, which should execute in O(n), not O(1) time. Is it then that System.arraycopy attempts to do something smarter than copying elements one by one into newly created array, since the time is

list接口实现类

99封情书 提交于 2019-12-21 10:45:22
一、ArrayList 1、概述 ArrayList是基于数组实现的List类,实现所有可选列表操作,允许所有元素包括null 2、初始化 ArrayList arrayList = new ArrayList(); =>初始容量为10的列表集合 ArrayList arrayList = new ArrayList(); =>数据类型为E,初始容量为10 3、主要方法 boolean add(E e) 将指定的元素追加到此列表的末尾。 void add(int index, E element) 在此列表中的指定位置插入指定的元素。 boolean addAll(Collection<? extends E> c) 按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾。 boolean addAll(int index, Collection<? extends E> c) 将指定集合中的所有元素插入到此列表中,从指定的位置开始。 boolean contains(Object o) 如果此列表包含指定的元素,则返回 true 。 E get(int index) 返回此列表中指定位置的元素。 E remove(int index) 删除该列表中指定位置的元素。 E set(int index, E element)

Pass Arraylist as argument to function [closed]

混江龙づ霸主 提交于 2019-12-21 07:02:46
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . I have an arraylist A of Integer type. I created it as: ArrayList<Integer> A = new ArrayList<Integer>(); Now, I want to pass it as an argument to

How to pass images into ArrayList<String> in Android?

耗尽温柔 提交于 2019-12-21 06:28:12
问题 I have a situation where I need to set a list of images and I am unable to do that cause I am not aware on how to pass a list of string array to a bitmap. CODE : private void setImageBit(String item, ImageView imageView){ path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "SanPics2"; File file = new File(path,"Image 5.jpg"); String item = file.toString(); Bitmap bitmap = BitmapFactory.decodeFile(item); float i = ((float) imageWidth) /

How to send an ArrayList of custom objects in a Bundle

大憨熊 提交于 2019-12-21 06:06:22
问题 I have an application that uses a service to create an ArrayList of custom objects (MyObject) every x seconds. I then want my Activity to obtain this ArrayList. I'm currently planning on having the Service send a message to the Activity's handler every time it finishes the query for the data. I want the message to the Handler to contain the ArrayList of MyObjects. When building the method in the Activity to get this ArrayList out of the message, I noticed that I couldn't. If I tried msg