系列文章目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用
提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、Collection集合
单例集合的顶层接口,它表示-组对象,这些对象也称为Collection的元素
●JDK 不提供此接口的任何直接实现, 它提供更具体的子接口(如Set和List)实现
创建Collection集合的对象
多态的方式
●具体的实现类ArrayList
1.1Collection集合遍历
Iterator:迭代器,集合的专用遍历方式
Iterator iterator(): 返回此集合中元素的迭代器,通过集合的iterator0方法得到
●
迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的
Iterator中的常用方法
●
E next0: 返回迭代中的下一个元素
●boolean hasNext():如果迭代具有更多元素,则返回true
1.2集合类体系结构
package demo01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import javax.print.attribute.SetOfIntegerSyntax;
public class Demo01Collection {
public static void main(String[] args) {
// Collection<String> coll= new ArrayList<>();//体现了多态
Collection<String> coll=new HashSet<>();
System.out.println(coll);
boolean b1=coll.add("张三");
System.out.println(b1);
System.out.println(coll);
coll.add("李四");
coll.add("李四");
coll.add("王五");
coll.add("赵六");
System.out.println(coll);
boolean b2=coll.remove("王五");
System.out.println(b2);
System.out.println(coll);
boolean b3=coll.contains("赵六");
System.out.println(b3);
System.out.println(coll);
boolean b4=coll.contains("王五");
System.out.println(b4);
boolean b5=coll.isEmpty();
System.out.println(b5);
int b6=coll.size();
System.out.println(b6);
Object[] arr=coll.toArray();
// for(int i=0;i<arr.length;i++)
{
System.out.println(0);
// }
System.out.println("===========");
coll.clear();
System.out.println(coll);
b5=coll.isEmpty();
System.out.println(b5);
}
}
}
}
}
}
package demo01;
//迭代器
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Deom02lterator {
public static void main(String[] args) {
Collection coll=new ArrayList<>();
coll.add(“姚明”);
coll.add(“科比”);
coll.add(“麦迪”);
coll.add(“杜兰特”);
coll.add(“詹姆斯”);
coll.add(“艾弗森”);
Iterator it=coll.iterator();
if(it.hasNext()) {
String e=it.next();
System.out.println(e);
}
if(it.hasNext()) {
String e=it.next();
System.out.println(e);
}
System.out.println("==========");
while(it.hasNext()) {
String e=it.next();
System.out.println(e);
}
System.out.println("==========");
for(Iterator<String> it2=coll.iterator();it2.hasNext();) {
String e=it2.next();
System.out.println(e);
}
System.out.println("=====第三次=====");
//增强for循环
for(String s:coll) {
System.out.println(s);
}
System.out.println("=====第四次=====");
int[] arr1= {4,5,6,7,8,9,10};
for(int i=0;i<arr1.length;i++) {
System.out.println(i);
}
for(int i:arr1) {
System.out.println(i);
}
}
}
二、List
1.List集合概述和特点
有集合(也称为序列), 胪可以精确控制列表中海个元素的插入位置。驴可以通过整数索引访问元素,
并搜索列表中的元素
●与Set集合不同, 列表通常允许重复的元素
List集合特点
●有序:存储和取出的元素顺序一 致
可重复:存储的元素可以重复
2.1List集合特有方法
Listterator
Listlterator:列表迭代器
●通过List集合的listlterator0方法得到, 所以说它是List集合特有的迭代器
●于允许程序员沿任一方向遍历列表的列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置
Listlterator中的常用方法
●
E next():返回迭代中的下一个元素
●
boolean hasNext():如果迭代具有更多元素,则返回true
●
E previous():返回列表中的上一个元素
●
boolean hasPrevious(:如果此列表迭代器在相反方向遍历列表时具有更多元素,则返回true
●void add(E e):将指定的元素插入列表
3.异常
3.1 try…catch…
执行流程:
程序从try面的代码开始执行
出现异常,会自动生成一个异常类对象,该异常对象将被提交给Java运行时系统
当Java运行时系统接收到异常对象时,会到catch中去找匹配的异常类,找到后进行异常的处理
执行完毕之后,程序还可以继续往下执行
package deom02;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//异常
public class Demo02Exception {
//编译期异常
public static void main(String[] args) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=null;
System.out.println("第一句测试");
try {
date=sdf.parse("2021-05-11");
} catch (ParseException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println(date);
System.out.println("第二句测试");
//runtimeException 运行期异常
int[] arr= {1,2,3};
System.out.println(arr[0]);
//有问题,但是不会提示,因为能够编译通过,在执行时才会有异常
try {
System.out.println(arr[2]);
}catch(Exception e) {
System.out.println(e);
System.out.println("第一句异常");
}
//error错误
int[] arr2= new int[1024*1024*1024*1024*104*1024*1024];
System.out.println("后续代码");
}
}
四Trow和Throws
格式:trows 异常类名
注意:这个格式是跟在方法的括号后面的
编译时异常必须要进行处理,两种处理方案: try…catch … .或者throws,如果采用throws这种方案,将来谁调用谁处理
运行时异常可以不处理,出现问题后,需要我们回来修改代码
Java中的异常被分为两大类:编译时异常和运行时异常,也被称为受检异常和非受检异常
所有的RuntimeException类及其子类被称为运行时异常,其他的异常都是编译时异常
编译时异常:必须显示处理,否则程序就会发生错误,无法通过编译
运行时异常:无需显示处理,也可以和编译时异常-样处理
package deom02;
//抛出异常throw
public class Demo02Throw {
public static void main(String[] args) {
int[] arr=null;
//int[] arr=new int[3];
int e=getElement(arr,3);
System.out.println(e);
}
public static int getElement(int[] arr, int index) {
if(arr==null) {
throw new NullPointerException(“传递的数组值是NULL”);
}
if(index<0||index>arr.length-1) {
throw new ArrayIndexOutOfBoundsException(“传递的索引超出了数组的正常使用范围”);
}
int ele=arr[index];
return ele;
}
}
package deom02;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo03Throws {
//public static void main(String[] args) throws FileNotFoundException,IOException{
//public static void main(String[] args) throws IOException
public static void main(String[] args) throws Exception{
readFile(“c:\a”);
System.out.println(“后续代码”);
}
public static void readFile(String fileName) throws FileNotFoundException,IOException {
if(!fileName.equals("c:\\a.txt")) {
throw new FileNotFoundException("传递的文件不是c:\\a.txt");
}
if(!fileName.endsWith(".txt")) {
throw new IOException("文件后缀名有无");
}
System.out.println("文件正确");
}
}
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。