arraylist

java.lang.StackOverflowError when trying to add the same instance of list multiple times

爷,独闯天下 提交于 2020-04-12 07:42:31
问题 How to resolve java.lang.StackOverflowError for the following code? Person.java import java.util.List; public class Person { private String name; private List<Person> children; public Person() { } public Person(String name, List<Person> children) { this.name = name; this.children = children; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Person> getChildren() { return children; } public void setChildren(List<Person> children) {

descendingIterator for java.util.List

五迷三道 提交于 2020-04-10 03:37:10
问题 LinkedList can be iterated using ascending or descending iterator like this: LinkedList<Object> list = new LinkedList<Object>(); ... StringJoiner sJ1 = new StringJoiner(" "); list.iterator().forEachRemaining(a -> sJ1.add(a.toString())); System.out.println("averse: \n" + sJ1.toString()); StringJoiner sJ2 = new StringJoiner(" "); list.descendingIterator().forEachRemaining(a -> sJ2.add(a.toString())); System.out.println("reverse: \n" + sJ2.toString()); averse: Hello 2 Chocolate 10 reverse: 10

ArrayList replace element if exists at a given index?

扶醉桌前 提交于 2020-04-07 13:55:34
问题 How to replace element if exists in an ArrayList at a given index? 回答1: arrayList.set(index i,String replaceElement); 回答2: If you're going to be requiring different set functionaltiy, I'd advise extending ArrayList with your own class. This way, you won't have to define your behavior in more than one place. // You can come up with a more appropriate name public class SizeGenerousArrayList<E> extends java.util.ArrayList<E> { @Override public E set(int index, E element) { this.ensureCapacity

【总结—.Net Framework集合类】

≡放荡痞女 提交于 2020-04-07 10:01:51
写在前面: 最近在学习 .Net 的集合框架,看了许多博文,也看了书,总感觉不是很明白。心里疑惑很多,每天郁闷度日。如果学技术时感到很痛苦,有两种可能,其一是学习方法不对;其二是真的不适合。希望大多数搞技术的朋友都不属于后者。方法不对,那得找解决方法呀,于是乎网上一顿狂搜,也没发现有价值的文章。同样地,我搜索了一下 Java 的集合类,文章一堆,图片更少满天飞。而 .Net 连一张像样的说明图片都没有,觉得微软的技术总是藏着掖着,放佛怕人知道似的。不知道各位有没有这样的想法 !? 不过话又说回来,这样也好,越是不想让我知道,我就越想知道,自己总结出来的东西才记忆深刻! 上面这幅图是 Java 的。 下面就把这几天的学习成果总结一下,与朋友们共享,有纰漏的地方还望指点! 集合,表示可以通过遍历每个元素来访问的一组对象 ( 特别是可使用 foreach 循环访问 ) 。一个集合包括多个元素,即有一个集合类对象和 N 个元素对象。 因为任何集合类都实现了 IEnumerable 接口,所以任何集合类对象都有一个 GetEnumerator() 方法,该方法可以返回一个实现了 IEnumerator 接口的对象,这个返回的 IEnumerator 对象既不是集合类对象,也不是集合的元素类对象,它是一个独立的类对象。通过这个对象,可以遍历访问集合类对象中的每一个元素对象

java软件开发人中经常误解区

这一生的挚爱 提交于 2020-04-07 10:01:32
数组(array)转成集合( ArrayList ) 错误:如经常这样处理 List<String> list = Array.asList(arr); Arrays.asList()方法返回的是Arrays里的私有静态内部类ArrayList,不是集合java.util.ArrayList类。这是java.util.Arrays.Arrys.ArrayList类内部只有三个方法 set(),get(),contains(),但没有增加元素方法,因此他大小是不变的。要想创建一个集合ArrayList,这样的如: ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(arr)); 检查一个数组包含这个值(check if an Array contains a value) 开发人员经常这样做: Set<String> set = new HashSet<String>(Arrays.asList(arr)); return set.contains(targetValue); 代码上多执行了几步,必需先转换成一个集合再给set; 可以这样写: Arrays.asList(arr).contains(targetValue); or for(String s:arr){if(s.equals

Collection集合框架

喜夏-厌秋 提交于 2020-04-07 08:17:20
一,Collection集合框架 在实际开发中,将使用的对象存储到特定数据结构的容器中,而JDK提供了这样的容器——集合框架,集合框架中包含了一系列不同数据结构的实现类。 1)Collection常用方法 ①int size():返回包含对象的个数, ②boolean isEmpty():返回是否为空, ③boolean contains(Object o):判断是否包含某指定对象, ④boolean add(E e) :向集合中添加对象, ⑤boolean remove(Object o):从集合中删除对象。 2)Collection 与Collections的区别? Collection是java.util下的接口,是各种集合的父接口,继承于他的接口有list,set。Collections是java.util下的类,是针对集合的帮助类,提供一系列的静态方法,对各种集合进行检索,排序,线程安全化操作。 二,List集合的实现类:ArrayList和LinkedList List接口是Collection的子接口,用于定义线性表数据结构,元素可以重复,有序的,有索引值。可以将list理解为存储对象的数组,只不过其元素可以动态的增加或减少。 1)List接口有两个实现类:ArrayList和LinkedList。分别用动态数组和链表的方式实现了List接口。List

设计模式-组合模式

╄→гoц情女王★ 提交于 2020-04-07 07:50:07
组合模式(Composite Pattern)也叫合成模式,有时又叫做部分-整体模式(Part-Whole), 主要是用来描述部分与整体的关系: 定义: Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly.(将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。) 举个最常见的例子,公司组织架构就是一个典型的树状结构(网上截取图): image.png 我们一般会这样设计组织架构,看代码实现 首先根节点IROOT /** * 根节点 * @author shuliangzhao * @Title: IRoot * @ProjectName design-parent * @Description: TODO * @date 2019/6/18 22:37 */ public interface IRoot { //得到总经理的信息 public String getInfo(); //总经理下边要有小兵,那要能增加小兵,比如研发部总经理,这是个树枝节点 public void

How to interlace two array lists?

浪子不回头ぞ 提交于 2020-04-07 05:04:12
问题 I am trying to develop a program that shuffles a deck by dividing the deck into two and then interlacing them. Class Deck represents a deck of 52 cards. There are two methods: Deck(int n) and Card drawCard() . Deck(int n) is the constructor. The parameter tells how many rounds the deck should be shuffled. In each round of shuffling, the whole deck is first divided into two sub-decks. The sub-decks are then interlaced into one whole deck. Some notes : To simplify the discussion, we assume the

How to interlace two array lists?

。_饼干妹妹 提交于 2020-04-07 05:04:10
问题 I am trying to develop a program that shuffles a deck by dividing the deck into two and then interlacing them. Class Deck represents a deck of 52 cards. There are two methods: Deck(int n) and Card drawCard() . Deck(int n) is the constructor. The parameter tells how many rounds the deck should be shuffled. In each round of shuffling, the whole deck is first divided into two sub-decks. The sub-decks are then interlaced into one whole deck. Some notes : To simplify the discussion, we assume the

【ArrayList】为什么java.util.concurrent 包里没有并发的ArrayList实现?

混江龙づ霸主 提交于 2020-04-07 04:12:52
为什么java.util.concurrent 包里没有并发的ArrayList实现? 问:JDK 5在java.util.concurrent里引入了ConcurrentHashMap,在需要支持高并发的场景,我们可以使用它代替HashMap。但是为什么没有ArrayList的并发实现呢?难道在多线程场景下我们只有Vector这一种线程安全的数组实现可以选择么?为什么在java.util.concurrent 没有一个类可以代替Vector呢? 答:我认为在java.util.concurrent包中没有加入并发的ArrayList实现的主要原因是: 很难去开发一个通用并且没有并发瓶颈的线程安全的List。 像ConcurrentHashMap这样的类的真正价值(The real point / value of classes)并不是它们保证了线程安全。而在于它们在保证线程安全的同时不存在并发瓶颈。举个例子,ConcurrentHashMap采用了锁分段技术和弱一致性的Map迭代器去规避并发瓶颈。 所以问题在于,像“Array List”这样的数据结构,你不知道如何去规避并发的瓶颈。拿contains() 这样一个操作来说,当你进行搜索的时候如何避免锁住整个list? 另一方面,Queue 和Deque (基于Linked List