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 是 ...
随机推荐
- [AngularJS] “路由”的定义概念、使用详解——AngularJS学习资料教程
这是小编的一些学习资料,理论上只是为了自己以后学习需要的,但是还是需要认真对待的 以下内容仅供参考,请慎重使用学习 AngularJS“路由”的定义概念 AngularJS最近真的很火,很多同事啊同学 ...
- Android平台接入Facebook登录
官方教程地址: https://developers.facebook.com/docs/android/getting-started 开发环境为Android Studio,官方要求SDK最低版本 ...
- 【node】node的核心模块---http模块,http的服务器和客户端
nodejs事件机制 ##### http服务器和客户端 node.js标准库提供了http模块,其中封装了一个高效的http服务器和一个简易的http客户端 HTTP服务器 1. http.crea ...
- AJAX跨域访问(从Tomcat8到Apache/Nginx/Spring Boot)
1.在Tomcat的Root目录下放入如下的文件 apache-tomcat-8.0.12X64\webapps\ROOT clientaccesspolicy.xml文件 <?xml vers ...
- EventBus 3.0源码解析
现在网上讲解EventBus的文章大多数都是针对2.x版本的,比较老旧,本篇文章希望可以给大家在新版本上面带来帮助. EventBus 是专门为Android设计的用于订阅,发布总线的库,用到这个库的 ...
- 如何从 GitHub 上下载单个文件夹
DownGit 好用记得回来点赞(建议***)
- JS DOM节点增删改查 属性设置
一.节点操作 增 createElement(name)创建元素 appendChild();将元素添加 删 获得要删除的元素 获得它的父元素 使用removeChild()方法删除 改 第一种方 ...
- WinForm自定义控件
[ToolboxBitmap(typeof(PropertyGrid))]//设置在工具箱中显示的小图标 public partial class ServiceManage : UserCo ...
- font-family:中文字体的英文名称 (宋体 微软雅黑)
宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...
- [翻译] NSDate-TimeAgo
NSDate-TimeAgo https://github.com/kevinlawler/NSDate-TimeAgo NSDate+TimeAgo has merged with DateTool ...
因为collection是所有这几类的超级接口,所以collection类的所有接口子类都需要重写。所以我们首先需要先学习collection类的所有接口。