GVKun编程网logo

ArrayList 和Double

16

本文将带您了解关于ArrayList和Double的新内容,另外,我们还将为您提供关于ArrayListal=newArrayList();有什么区别?和ArrayListal=newArrayLis

本文将带您了解关于ArrayList 和Double的新内容,另外,我们还将为您提供关于ArrayList al = new ArrayList();有什么区别?和ArrayList al = new ArrayList(0)?、ArrayList list = new ArrayList()在这个泛型为Integer的ArrayList中存放一个String类型的对象、arraylist sublist的易错用法、ArrayList 的 subList 方法带来的坑的实用信息。

本文目录一览:

ArrayList 和Double

ArrayList 和Double

来自http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ103:

具有下限的通配符看起来像“?super Type”,代表所有类型的族,它们是Type的超类型,包括Type类型。类型称为下限。

所以为什么

ArrayList<? super Number> psupn1 = new ArrayList<Number>();psupn1.add(new Double(2));

编译?

Double不是Number的超类型,而是Number的子类…

编辑1:

    ArrayList<? super Number> pextn1 = new ArrayList<Number>();    psupn1.add(new Integer(2));    psupn1.add(new Double(2));    psupn1.add(new Float(2));    for(Number n : psupn1){ // [Invalid] Number should be change to    // Object even if I can only add subtype of Number??    }

答案1

小编典典

您可以 _添加_一个Double,因为无论类型参数E是什么,它都可以保证是Number或超类型…这意味着您可以肯定地从转换DoubleE。您将无法执行以下操作:

Number x = psupn1.get(0);

虽然。

考虑一下,然后尝试创建在逻辑上将其破坏的列表。例如,您不能使用:

// InvalidArrayList<? super Number> psupn1 = new ArrayList<Integer>();psupn1.add(new Double(2));

因为Integer 既不Number父类型 也不 是父类型-它是子类。你可以写:

// ValidArrayList<? extends Number> psupn1 = new ArrayList<Integer>();

…因为那是相反的方式。那时您可以写:

Number x = psupn1.get(0);

因为列表中的任何元素都保证可以转换 Number。它涉及 需要转换的方式-转换为泛型类型参数或 从中 进行转换。

ArrayList al = new ArrayList();有什么区别?和ArrayList al = new ArrayList(0)?

ArrayList al = new ArrayList();有什么区别?和ArrayList al = new ArrayList(0)?

ArrayList al = new ArrayList();有什么区别?和ArrayList al = new ArrayList(0)?

答案1

小编典典

如果您查看API,它将显示ArrayList()-构造一个初始容量为10的空列表。

ArrayList(int initialCapacity)-构造一个具有指定初始容量的空列表。

ArrayList list = new ArrayList()在这个泛型为Integer的ArrayList中存放一个String类型的对象

ArrayList list = new ArrayList()在这个泛型为Integer的ArrayList中存放一个String类型的对象

java面试要点---ArrayList list = new ArrayList(); 在这个泛型为Integer的ArrayList中存放一个String类型的对象。

ArrayList list = new ArrayList(); 在这个泛型为Integer的ArrayList中存放一个String类型的对象。 
1.刚刚看到的时候,也是很纳闷后来仔细看了下,java的反射机制;
2.这个可以通过java的反射机制来实现;
3.下面是一个例子:
 package com.credream.refelect;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class TestFile {
public static void main(String[] args) throws Exception {
List<Integer> list = new ArrayList<Integer>(); //定义Integer泛型
String str = "abc"; 
Method[] method=list.getClass().getMethods();//取得list的所有方法
System.out.println(method.length);
for(int i=0;i<method.length;i++){
System.out.println(method[i]);//遍历打印list的方法
}
method[0].invoke(list, str);//通过 反射来执行 list的第一个方法,第一个是list对象,代表该对象的方法,第二个是方法参数:  就是list.add(str);
System.out.println(list.size());
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
 ------------------------------------------------------------------------------------------------------------------
运行结果:
 35
public boolean java.util.ArrayList.add(java.lang.Object)
public void java.util.ArrayList.add(int,java.lang.Object)
public java.lang.Object java.util.ArrayList.get(int)
public java.lang.Object java.util.ArrayList.clone()
public int java.util.ArrayList.indexOf(java.lang.Object)
public void java.util.ArrayList.clear()
public boolean java.util.ArrayList.contains(java.lang.Object)
public boolean java.util.ArrayList.isEmpty()
public int java.util.ArrayList.lastIndexOf(java.lang.Object)
public boolean java.util.ArrayList.addAll(int,java.util.Collection)
public boolean java.util.ArrayList.addAll(java.util.Collection)
public int java.util.ArrayList.size()
public java.lang.Object[] java.util.ArrayList.toArray(java.lang.Object[])
public java.lang.Object[] java.util.ArrayList.toArray()
public boolean java.util.ArrayList.remove(java.lang.Object)
public java.lang.Object java.util.ArrayList.remove(int)
public java.lang.Object java.util.ArrayList.set(int,java.lang.Object)
public void java.util.ArrayList.ensureCapacity(int)
public void java.util.ArrayList.trimToSize()
public int java.util.AbstractList.hashCode()
public boolean java.util.AbstractList.equals(java.lang.Object)
public java.util.Iterator java.util.AbstractList.iterator()
public java.util.List java.util.AbstractList.subList(int,int)
public java.util.ListIterator java.util.AbstractList.listIterator(int)
public java.util.ListIterator java.util.AbstractList.listIterator()
public java.lang.String java.util.AbstractCollection.toString()
public boolean java.util.AbstractCollection.containsAll(java.util.Collection)
public boolean java.util.AbstractCollection.removeAll(java.util.Collection)
public boolean java.util.AbstractCollection.retainAll(java.util.Collection)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
1
abc

arraylist sublist的易错用法

arraylist sublist的易错用法

注意:toIndex - high endpoint (exclusive) of the subList

就是说 toIndex是不取值的.

Exp:modelList.subList(0,  + 200)

取的是modelList的第0--199条共200个数据,第200是不取的.

截取部分官网说明,注意标红部分:

https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#subList(int,%20int)

public List<E> subList(int fromIndex,

              int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

      list.subList(from, to).clear();

 

Similar idioms may be constructed for indexOf(Object) and lastIndexOf(Object), and all of the algorithms in the Collections class can be applied to a subList.

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

Specified by:

subList in interface List<E>

Overrides:

subList in class AbstractList<E>

Parameters:

fromIndex - low endpoint (inclusive) of the subList

toIndex - high endpoint (exclusive) of the subList

Returns:

a view of the specified range within this list

Throws:

IndexOutOfBoundsException - if an endpoint index value is out of range (fromIndex < 0 || toIndex > size)

IllegalArgumentException - if the endpoint indices are out of order (fromIndex > toIndex)

ArrayList 的 subList 方法带来的坑

ArrayList 的 subList 方法带来的坑

最近在项目中遇到了一个问题,由一个对象序列化的结构,在反序列化时一直提示失败,真的百思不得其解啊。在对问题排查了好久之后,才发现是这个序列化的对象中的 list 调用了 ArrayList 的 sublist 方法存入导致的问题,真的是满满的坑,sublist 还是要慎重使用的啊,下面详细介绍下 sublist。

以下内容转自:ArrayList 的 subList 方法

List 接口中定义:

List<E> subList(int fromIndex, int toIndex);

英文注释:

复制代码
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the 
returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
The returned list supports all of the optional list operations supported by this list.
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a
range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list: list.subList(from, to).clear(); Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList. The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via
the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in
progress may yield incorrect results.)
复制代码

根据注释得知:

1,该方法返回的是父 list 的一个视图,从 fromIndex(包含),到 toIndex(不包含)。fromIndex=toIndex 表示子 list 为空

2,父子 list 做的非结构性修改(non-structural changes)都会影响到彼此:所谓的 “非结构性修改”,是指不涉及到 list 的大小改变的修改。相反,结构性修改,指改变了 list 大小的修改。

3,对于结构性修改,子 list 的所有操作都会反映到父 list 上。但父 list 的修改将会导致返回的子 list 失效。

4,tips:如何删除 list 中的某段数据:

list.subList(from, to).clear();

示例代码:

来自【Java 每日一题】20170105,就是看到这个题目才让我知道 list 的这个方法我没有接触过

复制代码
package ques;  
  
import java.util.ArrayList;  
import java.util.List;  
  
public class Ques0105 {  
  
    public static void main(String[] args) {  
        List<String> list = new ArrayList<String>();  
        list.add("a");  
  
        // 使用构造器创建一个包含list的列表list1  
        List<String> list1 = new ArrayList<String>(list);  
        // 使用subList生成与list相同的列表list2  
        List<String> list2 = list.subList(0, list.size());  
        list2.add("b");  
  
        System.out.println(list.equals(list1));  
        System.out.println(list.equals(list2));  
    }  
}
复制代码

返回结果如下:

可以发现,list2 为 list 的子 list,当 list2 发生结构性修改(list2.add ("b"))后,list 也发生相应改变,所以返回结果为 false 和 true

关于ArrayList 和Double的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于ArrayList al = new ArrayList();有什么区别?和ArrayList al = new ArrayList(0)?、ArrayList list = new ArrayList()在这个泛型为Integer的ArrayList中存放一个String类型的对象、arraylist sublist的易错用法、ArrayList 的 subList 方法带来的坑的相关信息,请在本站寻找。

本文标签: