arraylist

Java: this keyword preceded by class name

China☆狼群 提交于 2020-01-15 07:16:18
问题 I find a snippet in ArrayList.java from jdk 8: @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } The line: Object[] elementData = ArrayList.this.elementData; looks strange to me. I think ArrayList.this is equivalent to this

Java: this keyword preceded by class name

旧城冷巷雨未停 提交于 2020-01-15 07:16:12
问题 I find a snippet in ArrayList.java from jdk 8: @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } The line: Object[] elementData = ArrayList.this.elementData; looks strange to me. I think ArrayList.this is equivalent to this

Path Sum II

大憨熊 提交于 2020-01-15 06:16:05
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22 , 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ]DFS /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(root == null) return result; ArrayList<Integer>

ArrayList、LinkedList、Vector、CopyOnWriteArrayList的区别和源码分析

荒凉一梦 提交于 2020-01-15 05:42:19
1. ArrayList ArrayList 是一个数组队列,相当于 动态数组 。与Java中的数组相比,它的 容量能动态增长 。它继承于AbstractList,实现了List, RandomAccess, Cloneable, java.io.Serializable这些接口。 默认容量是10 (从源码中可以看出 每次容量扩大为原来的1.5倍 ,int newCapacity = oldCapacity + (oldCapacity >> 1);)。 ArrayList中的操作不是线程安全的! 所以, 建议在单线程中才使用ArrayList ,而在 多线程中可以选择Vector或者CopyOnWriteArrayList 。 public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { ...... /** * Default initial capacity. */ private static final int DEFAULT_CAPACITY = 10; public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this

ArrayList Search .net

落爺英雄遲暮 提交于 2020-01-15 05:05:08
问题 Following is the format of the data stored in my arraylist. A-Amsterdam B- Brussels C-Canada so and so forth. I wan to search my array list by passing just the first few characters till '-' So if i have something like AA-Test then i want to pass just 'AA' to check if it exists or not. I know that i can use contains or binarysearch but it does not serve my purpose as they both compare objects. Any suggestions?? thanks 回答1: You can solve this by creating your own IComparer and passing it into

装箱和拆箱

╄→尐↘猪︶ㄣ 提交于 2020-01-15 00:55:45
装箱和拆箱 1、装箱和拆箱   装箱转换是指将一个值类型隐式地转换成一个object 类型,或者把这个值类型转换成一个被该值类型应用的接口类型interface-type。把一个值类型的值装箱,也就是创建一个object 实例并将这个值复制给这个object。比如: int i = 10 ; object obj = i ; 装箱的过程 我们也可以用显式的方法来进行装箱操作: int i = 10 ; object obj = ( object ) i ; 2、拆箱转换 和装箱转换正好相反,拆箱转换是指将一个对象类型显式地转换成一个值类型,或是将一个接口类型显式地转换成一个执行该接口的值类型。 拆箱的过程分为两步:首先,检查这个对象实例,看它是否为给定的值类型的装箱值。然后,把这个实例的值拷贝给值类型的变量。 利用如下例子,查看一个对象拆箱的过程 。 int i = 10 ; object obj = i ; int j = ( int ) obj ; 可以看出拆箱过程正好是装箱过程的逆过程。必须注意,装箱转换和拆箱转换必须遵循类型兼容原则。 尽量避免装箱 我们之所以研究装箱和拆箱,是因为装箱和拆箱会造成相当大的性能损耗(相比之下,装箱要比拆箱性能损耗大),性能问题主要体现在执行速度和字段复制上。因此我们在编写代码时要尽量避免装箱和拆箱,常用的手段为: 1. 使用重载方法

Get filtered array size in AutoCompleteTextview

心已入冬 提交于 2020-01-14 19:22:05
问题 I am working on the project in which user can search data. For that, I have implemented AutoCompleteTextView . autoComplete.setAdapter(new ArrayAdapter<String>(CheckRiskActivity.this, R.layout.auto_text_row, druglist)); autoComplete.setThreshold(1); //druglist is my arraylist Text change listener is as below: autoComplete.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { // here I want to get the size of filtered array list every time when the

Java最新面试题

99封情书 提交于 2020-01-14 19:17:37
2019最新整理JAVA面试题附答案 作者:Jack 包含的模块: 本文分为十九个模块,分别是:Java 基础、容器、多线程、反射、对象拷贝、Java Web 、异常、网络、设计模式、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、MyBatis、RabbitMQ、Kafka、Zookeeper、MySQL、Redis、JVM 如下图所示: 共包含 208 道面试题,本文的宗旨是为读者朋友们整理一份详实而又权威的面试清单 ==================================================== 一. Java 基础模块 1.JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,Java 开发工具包,提供了 Java 的开发环境和运行环境。 JRE:Java Runtime Environment 的简称,Java 运行环境,为 Java 的运行提供了所需环境。 具体来说 JDK 其实包含了 JRE,同时还包含了编译 Java 源码的编译器 Javac,还包含了很多 Java 程序调试和分析的工具。简单来说:如果你需要运行 Java 程序,只需安装 JRE 就可以了,如果你需要编写 Java 程序,需要安装 JDK。 2.== 和 equals 的区别是什么

IndexOutOfBounds with Index 14, size 16. How?

江枫思渺然 提交于 2020-01-14 18:46:14
问题 How can the index be out of bounds when it actually is in bounds as shown by the stacktrace? Although the context may not matter we are working on a Lua parser/VM for an IDE on the Netbeans platform and this keeps creeping up. How can this be? Some strange concurrency issue? Thanks in advance for any insights. java.lang.IndexOutOfBoundsException: Index: 14, Size: 16 at java.util.ArrayList.rangeCheck(ArrayList.java:604) at java.util.ArrayList.get(ArrayList.java:382) at org.netbeans.lib.lexer

线程的同步控制synchronized和lock的对比和区别

戏子无情 提交于 2020-01-14 12:03:55
转载。 https://blog.csdn.net/wu1226419614/article/details/73740899 我们在面试的时候,时常被问到如何保证线程同步已经对共享资源的多线程编程。我们当然用同步代码块,同步方法,又或者是用java提供的锁机制来达到对共享资源变量的同步控制。 那么我们什么时候用synchronized,什么时候用lock,以及他们的区别是什么呢; 首先来说synchronized 是Java的关键字,是Java的内置特性,在JVM层面实现了对临界资源的同步互斥访问,通过对对象的头文件来操作,从而达到加锁和释放锁的目的。对象的头文件如下图: 那么synchronized的缺点是啥呢: 1)不能响应中断; 2)同一时刻不管是读还是写都只能有一个线程对共享资源操作,其他线程只能等待 3)锁的释放由虚拟机来完成,不用人工干预,不过此即使缺点也是优点,优点是不用担心会造成死锁,缺点是由可能获取到锁的线程阻塞之后其他线程会一直等待,性能不高。 而lock接口的提出就是为了完善synchronized的不完美的,首先lock是基于jdk层面实现的接口,和虚拟机层面不是一个概念;其次对于lock对象中的多个方法的调用,可以灵活控制对共享资源变量的操作,不管是读操作还是写操作; lock接口的关系图: