java集合(类似python的列表)
一:学习方法
我们在学习一个类的时候,如果他是期其他类的实现类,我们在学习的时候,先学习他的共同的继承类,学习他们共有的方法,在学习他实现类的特殊方法。由共性--->特殊。
二:集合
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的列表)的更多相关文章
- Java集合(二):List列表
在上一节中,介绍了Java集合的总体情况.从这节開始,将介绍详细的类.这里不单单介绍类的使用方法.还会试图从源代码的角度分析类的实现.这一节将介绍List接口及实现类.即列表中的链表LinkedLis ...
- Java集合源码 -- List列表
List概述 List是一个有序,可重复的集合,可以在List的中间插入和移除元素,根据整数索引访问元素 下图是List集合的框架图 下面是对上图的简单介绍 AbstractCollection: 提 ...
- Java集合(一):Java集合概述
注:本文基于JDK 1.7 1 概述 Java提供了一个丰富的集合框架,这个集合框架包括了很多接口.虚拟类和实现类. 这些接口和类提供了丰富的功能.可以满足主要的聚合需求. 下图就是这个框架的总体结构 ...
- Java集合-Python数据结构比较
Java list与Python list相比较 Java List:有序的,可重复的.(有序指的是集合中对象的顺序与添加顺序相同) Python list(列表)是有序的,可变的. Java Lis ...
- Python中列表,元组,字典,集合的区别
参考文档https://blog.csdn.net/Yeoman92/article/details/56289287 理解Python中列表,元组,字典,集合的区别 列表,元组,字典,集合的区别是p ...
- ES6中Set集合(与java里类似)
一.引入背景 Set集合是一种无重复元素的列表,开发者们一般不会逐一读取数组中的元素,也不太可能逐一访问Set集合中的每个元素,通常的做法是检测给定的值在某个集合中是否存在 Map集合内含多组键值对, ...
- python的列表和java的数组有何异同
今天面试被问到,自己学习一下. python的列表是可变长的,定义时不需要指定长度:pyhton是弱对象类型,python的列表存储的数据类型可以不相同:python的列表更加灵活,如可以通过''命令 ...
- Python基础(三)——集合、有序 无序列表、函数、文件操作
1.Set集合 class set(object): """ set() -> new empty set object set(iterable) -> n ...
- Java 集合 散列表hash table
Java 集合 散列表hash table @author ixenos 摘要:hash table用链表数组实现.解决散列表的冲突:开放地址法 和 链地址法(冲突链表方式) hash table 是 ...
随机推荐
- java-单例详解
java单例模式(Singleton)以及实现 一. 什么是单例模式 因程序需要,有时我们只需要某个类同时保留一个对象,不希望有更多对象,此时,我们则应考虑单例模式的设计. 二. 单例模式的特点 1. ...
- css之图像替换
time: 2016-03-30 20:00 这个月有点忙,学业的事工作的事私人的事有点烦,但是不能停止学习更不能忘记写博客! 最近看了<精通css>这本书,挑了一个点纪录一下. 一.含义 ...
- Burp Suite插件推荐
BurpSuiteHTTPSmuggler 网址 https://github.com/nccgroup/BurpSuiteHTTPSmuggler 作用 利用 中间件对 HTTP 协议的实现的特性 ...
- linux 软件包安装方式选择、安装位置、源码包安装
对外提供服务,比如apache,应使用源码包安装对内提供服务,比如gcc,只是我自己使用,使用rpm包安装 rpm包不需要指定安装位置,源码包的安装需要手动指定安装位置 rpm包默认安装位置/etc/ ...
- .hivehistory
在当前用户的家目录下有个.hivestory文件,里面存放了用户执行的hive操作记录,如下: [hadoop@hadoop1 hive-0.14]$ cat ~/.hivehistory show ...
- C++ 随机数字以及随机数字加字母生成
#include <time.h>#include <sys/timeb.h>void MainWindow::slot_clicked(){ QString strRand; ...
- Oracle EBS AR 更新客户
DECLARE l_return_status ); l_msg_count NUMBER; l_msg_data ); l_rec_type hz ...
- [C# | WinCE | Solution] 在 WinCE 上访问 SSL 加密后的 WCF SOAP 服务接口出现“未能与远程服务器建立信任关系”
Scenario: 服务器的 SOAP 使用了 GeoTrust 签名的 EV 证书,WinCE调用时出现“未能与远程服务器建立信任关系”的错误.原因是该 WinCE 设备信任的证书包括 Global ...
- RHEL7系统管理之网络管理
1. RHEL7的网络介绍 在RHEL7中, NetworkManager 提供的默认联网服务是一个动态网络控制和配置守护进程, 支持ifcfg类型的配置文件. NetworkManager 可用于连 ...
- C# Redis的操作
Nuget添加StackExchange.Redis的引用 由于Redis封装类同时使用了Json,需要添加JSON.NET引用(Newtonsoft.Json) Redis封装类 /// <s ...
因为collection是所有这几类的超级接口,所以collection类的所有接口子类都需要重写。所以我们首先需要先学习collection类的所有接口。