arraylist

生成括号

徘徊边缘 提交于 2020-02-09 04:01:32
给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。 样例 给定 n = 3 , 可生成的组合如下: "((()))", "(()())", "(())()", "()(())", "()()()" 1 public class Solution { 2 /** 3 * @param n n pairs 4 * @return All combinations of well-formed parentheses 5 */ 6 public ArrayList<String> generateParenthesis(int n) 7 { 8 ArrayList<String> ss = new ArrayList<String>(); 9 ss = print(n, n, ss, 0); 10 return ss; 11 } 12 13 ArrayList<String> print(int i, int j, ArrayList<String> ss, int p) 14 { 15 if (ss.size() == 0) 16 { 17 String s = "("; 18 ss.add(s); 19 i--; 20 } else 21 { 22 ArrayList<String> list = new ArrayList(); 23 if (p == 0)

ArrayList元素怎么去重的两种方法

佐手、 提交于 2020-02-08 23:55:22
方法1,通过Set实现类包裹一层返回,缺点是会打乱原有集合的顺序 public static <T> List<T> listRepeatUseSet(List<T> list){ HashSet<T> hashSet = new HashSet<>(list); return new ArrayList<>(hashSet); } 方法2,新建一个新的ArrayList集合,这里不能指定大小因为当前传递过来的集合是有重复的所以长度大小是不正确的,如果当前传递过来的ArrayList集合中的元素是自定义数据那么需要重写equals和hashCode方法,其实当前场景只需要重写equals方法因为要使用到ArrayList中的contains方法,而这个方法中底层是使用对象的equals方法做比较。 public static <T> List<T> listRepeatUseIteration(List<T> list){ List<T> arrayList = new ArrayList<>(); ListIterator<T> listIterator = list.listIterator(); while (listIterator.hasNext()) { T next = listIterator.next(); if(!arrayList.contains(next)

迭代器模式与Java Iterator源码

。_饼干妹妹 提交于 2020-02-08 16:13:43
迭代器模式 迭代器模式是一种将集合的增删改操作与集合的顺序遍历操作分离的设计模式。集合只负责增删改操作,迭代器对于集合的内部类,专门负责顺序遍历。Java的Iterator是迭代器模式的经典实现。笔者jdk版本是11.0.4,不同版本的jdk的Iterator相关类及其实现有所不同,下面以jdk11.0.4为例。 Iterator jdk定义了一个 Iterator 接口,声明了 hasNext , next , remove 方法,分别用于检查是否结束遍历、遍历下一个元素、删除元素的操作。 /** * An iterator over a collection. {@code Iterator} takes the place of * {@link Enumeration} in the Java Collections Framework. Iterators * differ from enumerations in two ways: * * <ul> * <li> Iterators allow the caller to remove elements from the * underlying collection during the iteration with well-defined * semantics. * <li> Method names have

List、Set、Map 三者区别

血红的双手。 提交于 2020-02-08 13:27:30
List 是可重复集合,Set是不可重复集合,这两个接口都实现了 Collection 父接口。 Map 未继承 Collection,而独立的接口,Map 是一种把键对象和值对象进行映射的集合,它的每一个元素都包含了一对键对象和值对象,Map中存储的数据是没有顺序的,其 key 是不能重复的,它的值是可以有重复的。 List 的实现类有 ArrayList、LinkedList、Vector、Stack(淘汰): ArrayList和 Vector 内部是线性动态数组结构,在查询效率上会高很多,Vector 是线程安全的,相比 ArrayList 线程不安全的,性能会稍慢一些。 LinkedList:是双向链表的数据结构存储数据,在做查询时会按照序号索引数据进行前向或后向遍历,查询效率较低,但插入数据时只需要记录本项的前后项即可,所以插入速度较快。 Set 的实现类有 HashSet 和 TreeSet: HashSet:内部是由哈希表(实际上是一个HashMap实例)支持的。它不保证 set 元素的迭代顺序。 TreeSet:TreeSet 使用元素的自然顺序对元素进行排序,或者根据创建 set 时提供的 Comparator 进行排序。 Map 接口有三个实现类:HashTable,HashMap,TreeMap,LinkedHashMap: HashTable

【java】ArrayList、Iterator用法

本小妞迷上赌 提交于 2020-02-08 11:25:01
1 package com.tn.collect; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 6 class Product{ 7 public Product() { 8 super(); 9 // TODO Auto-generated constructor stub 10 } 11 public Product(int id, String name, int price) { 12 // TODO Auto-generated constructor stub 13 setId(id); 14 setName(name); 15 setPrice(price); 16 } 17 private int id; 18 private String name; 19 private double price; 20 public int getId() { 21 return id; 22 } 23 public void setId(int id) { 24 this.id = id; 25 } 26 public String getName() { 27 return name; 28 } 29 public void setName(String name) { 30 this

强连通分量算法

半城伤御伤魂 提交于 2020-02-08 10:54:17
Kosaraju算法 ,Targen算法(递归) c# 1 代码 1 /* 2 * User: Answer 3 * Date: 2010-04-13 4 * Time: 21:58 5 */ 6 using System; 7 using System.IO; 8 using System.Collections; 9 using System.Text; 10 11 namespace Graph 12 { 13 /// <summary> 14 /// Description of SCCKosaraju. 15 /// </summary> 16 public class SCCKosaraju 17 { 18 ArrayList[] adja; // adjacency list 19 ArrayList[] Radja; // reverse adjacency list 20 bool [] visited; 21 Stack stack; 22 int count = 1 ; 23 24 public SCCKosaraju(ArrayList[] adja) 25 { 26 this .adja = adja; 27 this .Radja = ReverseAdjacencyList(adja); 28 this .stack = new Stack(); 29

Why arraylist doesn't return insertion order in Kotlin?

强颜欢笑 提交于 2020-02-08 10:04:37
问题 I am facing a weird problem here. I have an Array List in my fragment, and each time I add value to it I was expecting it to keep the insertion order. However, it doesn't keep this order when I print the arraylist, it gives a weird order which makes no sense at all. Is there something I am missing here regarding ArrayList, or Fragments in Kotlin or Android? Thanks in advance. Below part of my codes; class HourlySalesFragment : Fragment(){ var tt = arrayListOf<Double>() override fun

Why arraylist doesn't return insertion order in Kotlin?

假装没事ソ 提交于 2020-02-08 10:03:06
问题 I am facing a weird problem here. I have an Array List in my fragment, and each time I add value to it I was expecting it to keep the insertion order. However, it doesn't keep this order when I print the arraylist, it gives a weird order which makes no sense at all. Is there something I am missing here regarding ArrayList, or Fragments in Kotlin or Android? Thanks in advance. Below part of my codes; class HourlySalesFragment : Fragment(){ var tt = arrayListOf<Double>() override fun

第23讲:Strategy 策略模式

时间秒杀一切 提交于 2020-02-08 03:42:39
2006.9.25 李建忠 算法与对象的耦合 对象可能经常需要使用多种不同的算法,但是如果变化频繁,会将类型变得脆弱…… 动机(Motivation) 在软件构建过程中,某些对象使用的算法可能多种多样,经常改变,如果将这些算法都编码到对象中,将会使对象变得异常复杂;而且有时候支持不使用的算法也是一个性能负担。 如何在运行时根据需要透明地更改对象的算法?将算法与对象本身解耦,从而避免上述问题? 意图(Intent) 定义一系列算法,把它们一个个封装起来,并且使它们可互相替换。该模式使得算法可独立于使用它的客户而变化。 ——《设计模式》GoF 例说Strategy模式应用 这个程序有两个可能的变化点:当枚举类型增加时,即处理的方法增加,那么Process函数需要修改补充一个if else分支;当我们想对分支1的处理ProcessA进行更改时,也要对Process函数进行修改。 针对上面的问题,我们首先想到的是把ProcessA写成受保护的虚函数(在OO中我们一般把虚函数都写成受保护的函数,因为它是能改变类的行为的函数,一般情况下只应该作为子类和父类之间的协议出现)。 Strategy模式的设计 把Cart类和ProcessStrategy类作为对象组合的方式使用。IProcessStrategy表达的是一个算法抽象。 抽象和具体算法 客户程序 这样算法就可以动态地去改变了

Collection接口的常用方法

筅森魡賤 提交于 2020-02-08 01:49:47
一、Collection简述 Collection为集合的接口,JDK没有提供其实现类; 1、Collection具有两个比较常用的子接口,List和Set; 2、List接口用于存储有有序的可重复的元素,Set接口用于存储无序的不可重复的元素; 3、List接口比较常用的实现类有ArrayList和LinkedList,ArrayList的底层实现是数组,储存在一段连续的内存空间中,具有索引,具有增删慢和查询快的特点; LinkedList的实现是在其内部具有Node的内部类,该类具有element、previous、next成员变量,在LinkList中添加元素时,会创建一个Node的对象并初始化这三个成员变量,因此可以从LinkedList中的任何一个元素找到其向上和向下的相邻的元素,LinkList具有增删快、查询慢的特点; 4、Set接口的比较常用的实现类是HashSet,其实现原理是数组、链表和哈希表(数组上的每个位置是一个链表), 在添加元素时计算该元素的哈希值确定在数组上的位置,然后检查该位置上是否有元素,如果没有,则将该元素添加在该位置,如果该位置有元素,则逐一检查该位置上的链表中是否有盖元素,如果没有则添加到最后一个节点,否则不添加; 2、Collection中的常用方法 1、增   add(E e)---boolean,向集合中添加元素e