一:学习方法

我们在学习一个类的时候,如果他是期其他类的实现类,我们在学习的时候,先学习他的共同的继承类,学习他们共有的方法,在学习他实现类的特殊方法。由共性--->特殊。

二:集合

1、集合和数组区别:

 package test07;

 import java.util.ArrayList;
import java.lang.Iterable;
import java.util.Iterator; public class arr_test {
public static void main(String ...args){
arr_Test();
set_test();
} public static void arr_Test(){ //数组
int[] a={,,,,};
for (int i:a){
System.out.print(i); }
}
public static void set_test(){
ArrayList<Integer> list_in=new ArrayList<Integer>();//集合 需要引用类型
list_in.add();
list_in.add();
list_in.add();
list_in.add();
Iterator<Integer> list_it= list_in.iterator();
while (list_it.hasNext()){
System.out.print(list_it.next());
}
} }

区别:

1、数组的一旦定义之后,初始化之后长度不可以改变。

2、集合内的元素为引用类型,基本类型需要使用其包装类进行进行操作,长度可变。

集合是java提供一种容器,为了存储多个数据。

2、ArrayList的继承关系:

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

public interface List<E>
extends Collection<E>
public abstract class AbstractCollection<E>
extends Object
implements Collection<E>

ArrayList继承AbstractList抽象类  实现List接口,而List接口继承Collection超级接口。而类AbstractList 继承AbstractCollection抽象类。

继承Collection类的所有子类对其方法都进行重写,而超级接口Collection 的子类有Set 和List

public interface Set<E>
extends Collection<E>
public interface List<E>
extends Collection<E>

LIst常用的子类:

ArrayList类、LinkedList类

public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable
Set接口常用的子类:
HashSet类、LinkedHashSet类
public class LinkedHashSet<E>
extends HashSet<E>
implements Set<E>, Cloneable, Serializable
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, Serializable
关系图:
   

因为collection是所有这几类的超级接口,所以collection类的所有接口子类都需要重写。所以我们首先需要先学习collection类的所有接口。
Collection常用的方法如下:
 package test07;

 import org.omg.Messaging.SYNC_WITH_TRANSPORT;

 import javax.swing.text.html.HTMLDocument;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator; public class Collect_demo {
public static void main(String...args){
add_demo();
itor_demo();
remov_Demo();
}
public static void add_demo(){
/*
*给集合添加元素。
* bool add(E e)
* 返回值为布尔类型,添加成功为true反之为false;
* int size() 返回集合的长度。
* bool contains(object) 是否包含元素。返回布尔值.
* void clear() 移除collection所有元素.无返回值.
*/
Collection<Integer> co_in=new ArrayList<>();
co_in.add();
co_in.add();
boolean ret=co_in.add();
int set_len=co_in.size();
System.out.print(set_len);
System.out.print(co_in);
System.out.print(ret);
System.out.print("+++++++++");
System.out.print(co_in.contains());
System.out.print("+++++++++");
co_in.clear();
System.out.print(co_in); }
public static void itor_demo(){
/*
因为collection 继承Iterator 其中
Iterator<E> iterator()返回值为iterator迭代器对象,所以根据这个对象 进行循环。
其中 bool hasNext()返回布尔值,表示当前的迭代器是否有下个元素。
e next()返回下个元素。
object[] toArray(collection)
*/
Collection<Integer> it=new ArrayList<>();
it.add();
it.add();
it.add();
it.add();
Iterator<Integer> it_or=it.iterator();
while (it_or.hasNext()){
System.out.print(it_or.next()+"\n");
}
Integer[] it_a=new Integer[it.size()];
it.toArray(it_a);
System.out.print(it_a.length);
}
public static void remov_Demo(){
Collection<Integer> re_cl=new ArrayList<>();
/*
bool remove(object)
移除指定对象。返回值为布尔值
Collection<E> removeall(Collection<?> e)
移除传入Collection对象的内的值,如果传入的对象内值在被移除的集合,那么被移除,否则不移除。也可以出入原集合。
bool isEmpyt()返回布尔值,判断集合是否为空.
*/
re_cl.add();
re_cl.add();
re_cl.add();
re_cl.add();
System.out.print(re_cl);
boolean ret=re_cl.remove();
Collection<Integer> a=new ArrayList<Integer>();
a.add();
a.add();
boolean ret_1=re_cl.removeAll(a);
boolean ret_2=re_cl.removeAll(re_cl);
System.out.print(re_cl);
System.out.print(ret);
System.out.print("------------");
System.out.print(re_cl);
if(re_cl.isEmpty()){
System.out.print("tis set is empty!");
}
}
// public static void
}

java获取长度三种方式:

1、字符串 length()

2、数组  length

3、集合  size()

 package test07;

 import java.util.ArrayList;

 public class test {
public static void main(String...args){
func_1();
}
public static void func_1(){
String a="";
System.out.print(a.length());
int[] a_ar=new int[];
a_ar[]=;
a_ar[]=;
a_ar[]=;
System.out.print(a_ar.length);
ArrayList<Integer> test=new ArrayList<>();
test.add();
test.add();
test.add();
test.add();
System.out.print(test.size());
}
}

java集合(类似python的列表)的更多相关文章

  1. Java集合(二):List列表

    在上一节中,介绍了Java集合的总体情况.从这节開始,将介绍详细的类.这里不单单介绍类的使用方法.还会试图从源代码的角度分析类的实现.这一节将介绍List接口及实现类.即列表中的链表LinkedLis ...

  2. Java集合源码 -- List列表

    List概述 List是一个有序,可重复的集合,可以在List的中间插入和移除元素,根据整数索引访问元素 下图是List集合的框架图 下面是对上图的简单介绍 AbstractCollection: 提 ...

  3. Java集合(一):Java集合概述

    注:本文基于JDK 1.7 1 概述 Java提供了一个丰富的集合框架,这个集合框架包括了很多接口.虚拟类和实现类. 这些接口和类提供了丰富的功能.可以满足主要的聚合需求. 下图就是这个框架的总体结构 ...

  4. Java集合-Python数据结构比较

    Java list与Python list相比较 Java List:有序的,可重复的.(有序指的是集合中对象的顺序与添加顺序相同) Python list(列表)是有序的,可变的. Java Lis ...

  5. Python中列表,元组,字典,集合的区别

    参考文档https://blog.csdn.net/Yeoman92/article/details/56289287 理解Python中列表,元组,字典,集合的区别 列表,元组,字典,集合的区别是p ...

  6. ES6中Set集合(与java里类似)

    一.引入背景 Set集合是一种无重复元素的列表,开发者们一般不会逐一读取数组中的元素,也不太可能逐一访问Set集合中的每个元素,通常的做法是检测给定的值在某个集合中是否存在 Map集合内含多组键值对, ...

  7. python的列表和java的数组有何异同

    今天面试被问到,自己学习一下. python的列表是可变长的,定义时不需要指定长度:pyhton是弱对象类型,python的列表存储的数据类型可以不相同:python的列表更加灵活,如可以通过''命令 ...

  8. Python基础(三)——集合、有序 无序列表、函数、文件操作

    1.Set集合 class set(object): """ set() -> new empty set object set(iterable) -> n ...

  9. Java 集合 散列表hash table

    Java 集合 散列表hash table @author ixenos 摘要:hash table用链表数组实现.解决散列表的冲突:开放地址法 和 链地址法(冲突链表方式) hash table 是 ...

随机推荐

  1. 左连接sql

    <?php public function sumZong($id){ $sql =' SELECT * FROM vvt_league_user AS p Left join vvt_leag ...

  2. elastic job will never fire

    1. 描述 2018-08-20 18:11:01.912 [Thread-8] INFO  org.quartz.impl.StdSchedulerFactory - Using default i ...

  3. HTML 5 <input> placeholder 属性 实现搜索框提示文字点击输入后消失

    H5之前要实现这个功能还要用到JS,H5出来之后新增加了placeholder属性,有了这个属性就就能轻松实现这个功能. 定义和用法 placeholder 属性提供可描述输入字段预期值的提示信息(h ...

  4. elasticsearch 多列 聚合(sql group by)

    文档数据格式 {"zone_id":"1","user_id":"100008","try_deliver_t ...

  5. Android分享---调用系统自带的分享功能

    以前我们总想到友盟等平台分享功能的集成,集成这玩意还得下载对应的jar包.当然,用这些平台的分享并不是说什么好处都没有,至少人家的统计功能还是很实用的.不过有的时候我们是不需要多余功能的,只需要能分享 ...

  6. 精华阅读第 12 期 | 最新 App Store 审核指南与10大被拒理由?

    很多时候,我们对技术的追求是没有止境的,我们需要不断的学习,进步,再学习,再进步!本文系移动精英开发俱乐部的第12期文章推荐阅读整理,其中涉及到了 Android 数据库框架,架构设计中的循环引用,同 ...

  7. leetCode之二叉树数中序遍历(递归实现)

    1.题目描述 2.分析 对于树来说,由于其结构是递归定义的,所以对二叉树很多算法使用递归是最容易的.反倒是使用循环方式需要借助特殊的数据结构来实现. 3.代码 vector<int> in ...

  8. jquery中的ajax方法

    $.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Strin ...

  9. STL标签与EL表达式之间的微妙关系

    很高兴,今天能和大家分享刚学的一些新知识.我们在java开发过程中经常会在jsp中嵌入一些java代码,比如<%=request.getParameter("id")%> ...

  10. QQ邮箱验证码

    人的记忆有时候跟鱼一样,只有七秒钟,短暂的时间! .NET  Web窗体实现忘记密码,使用QQ邮箱验证修改 一.首先设置一下发送个人或企业发送的邮箱 二.登录邮箱进行设置,如图:  三.关闭邮箱 四. ...