arraylist

为什么Java中继承是有害的

喜夏-厌秋 提交于 2019-12-19 03:28:42
本文转载自:http://www.javajia.com 概述   大多数好的设计者象躲避瘟疫一样来避免使用实现继承(extends 关系)。实际上80%的代码应该完全用interfaces写,而不是通过extends。“JAVA设计模式”一书详细阐述了怎样用接口继承代替实现继承。这篇文章描述设计者为什么会这么作。   Extends是有害的;也许对于Charles Manson这个级别的不是,但是足够糟糕的它应该在任何可能的时候被避开。“JAVA设计模式”一书花了很大的部分讨论用interface继承代替实现继承。   好的设计者在他的代码中,大部分用interface,而不是具体的基类。本文讨论为什么设计者会这样选择,并且也介绍一些基于interface的编程基础。 接口(Interface)和类(Class)    一次,我参加一个Java用户组的会议。在会议中,Jams Gosling(Java之父)做发起人讲话。在那令人难忘的Q&A部分,有人问他:“如果你重新构造Java,你想改变什么?”。“我想抛弃classes”他回答。在笑声平息后,它解释说,真正的问题不是由于class本身,而是实现继承(extends 关系)。接口继承(implements关系)是更好的。你应该尽可能的避免实现继承。 失去了灵活性   为什么你应该避免实现继承呢

Unsafe or unchecked operations for ArrayList

南笙酒味 提交于 2019-12-19 02:58:07
问题 I'm been assigned to make a program that gets 100 random integers between 0-25 and store them in an array. I then have to call upon 2 methods to split the evens and the odds (very typical). So I tried the ArrayList thing (I jut learnt it) and it seemed fine (I was following tutorial and things online) until I ran into this: Unit8.java uses unchecked or unsafe operations My code is this: import java.util.*; import java.awt.*; public class Unit8 { public static void main (String [] args) { /

3-链表篇(3)

北城余情 提交于 2019-12-19 02:12:19
附1:Stack API 附2:ArrayList API 题一:输入一个链表,按链表从尾到头的顺序返回一个ArrayList。 法一:递归 1 /** 2 * public class ListNode { 3 * int val; 4 * ListNode next = null; 5 * 6 * ListNode(int val) { 7 * this.val = val; 8 * } 9 * } 10 * 11 */ 12 import java.util.ArrayList; 13 public class Solution { 14 ArrayList list = new ArrayList(); 15 public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { 16 if(listNode!=null){ 17 printListFromTailToHead(listNode.next); 18 list.add(listNode.val); 19 } 20 return list; 21 } 22 } 法二:Stack 1 import java.util.*; 2 public class Solution { 3 ArrayList list = new ArrayList();

2020年Java面试100题

别来无恙 提交于 2019-12-19 01:54:10
一、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 的区别是什么? == 解读 对于基本类型和引用类型 == 的作用效果是不同的,如下所示: 基本类型:比较的是值是否相同; 引用类型:比较的是引用是否相同; 代码示例: String x = "string"; String y = "string"; String z = new String("string"); System.out.println(x==y); // true System.out.println(x==z); // false System.out.println(x.equals(y)); // true System.out.println(x.equals(z));

ArrayList源码分析(jdk 1.8)

点点圈 提交于 2019-12-19 01:43:26
写在前面 ArrayList相信大家做开发的同学都不陌生,在开发过程中这应该是最常用的数据结构了吧。但是现在是“源码时代”,会用还不够,要知道他的实现原理,本文主要基于jdk1.8对ArrayList源码进行分析。 一、从主要字段开始 值得注意的是,ArrayList内部会有一个modCount字段,但是这个字段是在父类AbstractList中的,代表着修改次数,后面会讲 /** * 默认容量 */ private static final int DEFAULT_CAPACITY = 10; /** * 空元素集,构造函数传入空集合的时候使用 */ private static final Object[] EMPTY_ELEMENTDATA = {}; /** * 空元素集,默认无参构造函数中使用 */ private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; /** ArrayList真正保存元素的数组,在第一次插入元素的时候扩容 * */ transient Object[] elementData; // non-private to simplify nested class access /** * 当前数组元素个数 * * @serial */ private int size;

how to remove arraylist value in elastic search using curl?

我怕爱的太早我们不能终老 提交于 2019-12-19 00:40:09
问题 How to remove arraylist values in Elasticsearch using sense console or curl? i want to remove any array element.? POST /q/q/ { "a": [ "z", "q", "1" ] } it doesnt work for me: POST /q/q/AV4sjk40mWHLgYFNkmNd/_update { "script": { "lang": "painless", "inline": "ctx._source.a -=newsupp", "params": { "newsupp": "p" } } } or POST /q/q/AV4sjk40mWHLgYFNkmNd/_update { "script": { "lang": "painless", "inline": "ctx._source.a.remove("1")" } } 回答1: If you want to remove all occurrences in the list, you

java集合常见问题

别等时光非礼了梦想. 提交于 2019-12-19 00:09:29
Java集合 常见的集合问题 HashMap与HashTable的区别 HashMap的put方法的具体流程? HashMap 解决哈希冲突 什么是哈希? 什么是哈希冲突? HashMap为什么不直接使用hashCode()处理后的哈希值直接作为table的下标? HashMap在JDK1.7和JDK1.8中有哪些不同? 为什么HashMap中String、Integer这样的包装类适合作为K? ConcurrentHashMap和Hashtable的区别? Java集合的快速失败机制 “fail-fast” ArrayList 和 Vector 的区别? ArrayList和LinkedList的区别? Array 和 ArrayList 有什么区别?什么时候该应 Array 而不是 ArrayList 呢? HashSet是如何保证数据不可重复的? BlockingQueue是什么? 转自 https://zhuanlan.zhihu.com/p/82714518 常见的集合问题 Map 接口和 Collection 接口是所有集合框架的父接口: Collection 接口的子接口包括: Set 接口和 List 接口 Map 接口的实现类主要有: HashMap 、 TreeMap 、 Hashtable 、 ConcurrentHashMap 以及 Properties 等

How can I create an ArrayList with a starting index of 1 (instead of 0)

大城市里の小女人 提交于 2019-12-18 20:55:47
问题 How can I start the index in an ArrayList at 1 instead of 0? Is there a way to do that directly in code? (Note that I am asking for ArrayList , for plain arrays please see Initializing an array on arbitrary starting index in c#) 回答1: The obvious way to do it would be to wrap an ArrayList in your own implementation, and subtract 1 from the indexer in all operations that uses the index. You can in fact also declare an array in .NET, that has an arbitrary lower bound (Scroll down to the section

How can I create an ArrayList with a starting index of 1 (instead of 0)

半世苍凉 提交于 2019-12-18 20:55:32
问题 How can I start the index in an ArrayList at 1 instead of 0? Is there a way to do that directly in code? (Note that I am asking for ArrayList , for plain arrays please see Initializing an array on arbitrary starting index in c#) 回答1: The obvious way to do it would be to wrap an ArrayList in your own implementation, and subtract 1 from the indexer in all operations that uses the index. You can in fact also declare an array in .NET, that has an arbitrary lower bound (Scroll down to the section

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object in Android passing ArrayList object

房东的猫 提交于 2019-12-18 19:30:50
问题 I want to pass my ArrayList object to another activity, using a DataWrapper that implements Serializable . I followed the answer provided here: Pass arraylist of user defined objects to Intent android. I am starting the another Activity from MPAndroidChart library PieChart 's OnChartGestureListener() . This is how I passed my ArrayList object threadList : mChart.setOnChartGestureListener(new OnChartGestureListener() { @Override public void onChartSingleTapped(MotionEvent me) { Intent intent =