当前位置:网站首页>细学常用类,集合类,IO流
细学常用类,集合类,IO流
2022-08-02 11:05:00 【编程老哥】
常用类就是API(Application Programming Interface)指的是应用程序编程接口。
String 和 StringBuffer 类 来封装字符串
使用字符串常量直接初始化一个String对象
String str1 = "abc";
public class String_1 {
public static void main(String[] args) {
showString();
showString1();
showString2();
}
public static void showString(){
String str1 = new String();//空字符串
String str2 = new String("abc");
char[] char1 = new char[]{'d', 'e', 'f'};//数组字符串
String str3 = new String(char1);
System.out.println(str1+str2+str3.isEmpty());
}
public static void showString1(){
String str3 = "2113";
String str4 = " abcdefgedcba";
System.out.println(str4.length());
System.out.println(str4.indexOf('c'));
char[] char2 = str4.toCharArray();//字符串转化为字符数组
for (int i = 0; i < char2.length; i++) {
if (i != char2.length-1) {
System.out.print(char2[i] + ",");
}else {
System.out.print(char2[i]);
}
}
System.out.println(str4.toUpperCase());
System.out.println(String.valueOf("12"));
System.out.print(str4.replace("a", "/"));//替换
System.out.println(str4.trim());//去除两端空格
System.out.println(str3.isEmpty()+""+str4.contains("abc"));
}
protected static void showString2(){
String str1 = "abc";//直接声明一个字符串
String str2 = new String("abc");//创建了一个内容为abc的字符串
String str3 = "abc";
String str4 = new String("abc");
if (str1==str2){
System.out.println(true);
}else{
System.out.println(false);
}
if (str1==str3){
System.out.println(true);
}else{
System.out.println(false);
}
if (str2==str4){
System.out.println("3"+true);
}else {
System.out.println("3"+false);
}
System.out.println(""+str1.equals(str2));
}
}
.substring()截取 .split()分割
String Buffer类似一个字符容器,当在其中添加或删除字符时,并不会产生新的StringBuffer对象。
StringBuffer[多线程,效率高,不安全]与StringBulider[单线程,效率低,安全]
package String;
public class StringBuffer_1 {
public static void main(String[] args) {
add();
remove();
alter();
StringBuilder1();
}
private static void add(){
StringBuffer sb = new StringBuffer();
sb.append("abcdefg");
sb.insert(2,123);
sb.insert(4,"der");
System.out.println(sb);
}
private static void remove(){
StringBuffer sb = new StringBuffer("abcdefg");
sb.delete(1,3);
System.out.println(sb);
sb.deleteCharAt(3);
System.out.println(sb);
}
private static void alter(){
StringBuffer sb = new StringBuffer("abcdef");
sb.setCharAt(1,'p');
System.out.println("the points setChaAT "+sb);//指定修改
sb.replace(2,4,"qqq");
System.out.println("replace two object "+sb);
System.out.println("reverse the char "+sb.reverse());
}
public static void StringBuilder1(){
StringBuilder apple = new StringBuilder("apple");
apple.append("aa");
System.out.println(apple);
apple.toString();
System.out.println(apple);
apple.delete(1,3);
apple.deleteCharAt(2);
apple.insert(1,"3");
apple.replace(1,2,"11");
}
}
System类与Runtime类
System类它所提供的属性和方法是静态的,引用属性和方法,直接调用即可
getProperties()方法用于获取当前系统的全部属性,该方法返回一个Properties对象,其中封装了系统的所有属性,这些属性都是以键值对的形式存在
currentTimeMillis()方法返回的是一个long型
aaraycopy将一个数组中的元素快速拷贝到另一个数组
import java.util.Enumeration;
import java.util.Properties;
public class System_1 {
public static void main(String[] args) {
getProperties1();
arraycopy1();
}
protected static void getProperties1(){
Properties properties = System.getProperties();//get all system's key return enumeration object
Enumeration propertyNames = properties.propertyNames();
while (propertyNames.hasMoreElements()){
String key = (String) propertyNames.nextElement();
String value = System.getProperty(key);
System.out.print(key="------>"+value);
}
}
protected static void arraycopy1(){
int[] formArray = {0, 1,2,3,4,5,6};
int[] toArray = { 10,11,22,33,44,55,66};
System.arraycopy(formArray,2,toArray,2,3);
for (int i = 0; i < toArray.length; i++) {
System.out.println(formArray[i]+":"+toArray[i]);
}
}
}
runtime类表示虚拟机运行状态,用于封装jvm虚拟机
Math与Random
包装类:将基本数据类型的值包装为引用数据类型的对象
Date、Calendar、DateFormat
Collection:单列集合的根接口,用于存储一系列符合某种规则的元素,两个重要的接口List和Set,List是元素有序,元素可重复,Set是元素无序,元素不可重复,List接口的实现类ArrayList LinkedList Vector ,Set接口的实现类有HashSet [ contains _LinkedHashSet] TreeSet
ArrayList内部封装了一个长度可变的数组对象,当存入的元素超过数组长度时,ArrayList会在内存中分配一个更大的数组来储存这些元素,ArrayList可以看做是一个长度可变的数组。
import java.util.ArrayList;
public class ArrayList_1 {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("stu1");
list.add("stu2");
list.add("stu3");
list.add("stu4");
System.out.println("collection_long "+list.size()+"the second element "+list.get(1));
}
}//结果为4,stu2
ArrayList查询元素时速度很快,增删元素时效率较低。
LinkedList内部维护了一个双向循环链表,链表的每一个元素都使用引用的方式来记住它的前一个元素和后一个元素,从而将所有的元素彼此连接起来。当插入一个新元素时,只需要修改元素之间的这种引用关系即可,删除一个节点也是如此。正因为这样的储存结构,所以LinkedList集合添加元素和删除元素的有很高的效率。
import java.util.LinkedList;
public class LinkedList_1 {
public static void main(String[] args) {
LinkedList<Object> link = new LinkedList<>();
link.add("stu1");
link.add("stu2");
link.add("stu3");
link.add("stu4");
System.out.println(link.toString());
link.add(3,"Student");
link.addFirst("Frist");
System.out.println(link);
link.remove(3);//移除指定位置
link.removeFirst();
System.out.println(link);
}
}
Iterator与Collection\Map接口有所不同,Collection与Map接口用于存储元素,Iterator用于访问迭代即遍历Collection中的元素,因此Iterator对象也成为迭代器。
import java.util.ArrayList;
import java.util.Iterator;
public class Iterator_1 {
public static void main(String[] args) {
ArrayList<Object> list = new ArrayList<>();
list.add("data1");
list.add("data1");
list.add("data1");
list.add("data1");
Iterator<Object> iterator = list.iterator();
while (iterator.hasNext()){
Object obj = iterator.next();
System.out.println(obj);
}
for (Object onj1:list
) {
System.out.println(onj1);
}
forEach();
}
static String[] strs = {"aaa","bbb","ccc"};
protected static void forEach(){
for (String string:strs
) {
}
System.out.println(strs[0]+strs[1]+strs[2]);//foreach修改循环后的数组
//for循环遍历数组
for (int i = 0; i < strs.length; i++) {
strs[i] = "ddd";
}
System.out.println(strs[0]+strs[1]+strs[2]);
}
}/*data1
data1
data1
data1
data1
data1
data1
data1
aaabbbccc
ddddddddd*/
HashSet是根据哈希值来确定元素在集合中存储的位置,TreeSet是以二叉树的方式来存储元素
HashSet所存储的元素是不可重复的,并且元素都是无序的。HashSet集合添加一个对象时,先调用改对象的hashCode()方法来确定元素的存储位置,再调用对象的equals()的方法来确保该位置没有重复元素。
import java.util.HashSet;
import java.util.Iterator;
public class HashSet_1 {
public static void main(String[] args) {
Example1();
Example2();
}
protected static void Example1(){
HashSet<Object> set = new HashSet<>();
set.add("Jack");
set.add("eve");
set.add("rose");
set.add("rose");
Iterator<Object> iterator = set.iterator();
while (iterator.hasNext()){
Object nex1t = iterator.next();//如果有元素,通过迭代器的next()方法获取元素
System.out.print(nex1t);
}
}
public static void Example2(){
HashSet hs = new HashSet();
Student stu1 = new Student("1", "Jack");
Student stu2 = new Student("1", "eve");
Student stu3 = new Student("1", "rose");
hs.add(stu1);
hs.add(stu2);
hs.add(stu3);
System.out.println(hs);
}
}
class Student{
String id;
String name;
public Student(String id,String name){
this.id = id;
this.name = name;
}
public String toString(){
return id+ ":"+ name;
}
}//everoseJack[1:rose, 1:eve, 1:Jack]
TreeSet 排序二叉树 平衡二叉树
Map:双列集合的根接口,用来存储Key和Value,就像根据学生的一个学号可以找到对应的学生,Map接口主要有HashMap TreeMap HashTable.
Key 和 Value 对象之间的关系成为映射
import java.util.*;
public class HashMap_1 {
public static void main(String[] args) {
Example();
}
public static void Example(){
HashMap map = new HashMap();
map.put("1","Jack");
map.put("2","eve");
map.put("3","rose");
Set entrySet = map.entrySet();
Iterator iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) (iterator.next());//获取集合中键值对映射关系
Object key = entry.getKey(); //获取Entry中的键
Object value = entry.getValue(); // 获取EntryVALUE
System.out.print(key+"+"+value);//1+Jack2+eve3+rose
}
Collection values = map.values();
Iterator it = values.iterator();
while (it.hasNext()){
Object value1 = it.next();
System.out.print(value1);//Jackeverose
}
Set keySet = map.keySet();
Iterator iterator1 = keySet.iterator();
while (iterator1.hasNext()){
Object key2 = iterator1.next();
Object value2 = map.get(key2);
System.out.print(" "+key2+" "+value2);//1 Jack 2 eve 3 rose
}
}
}
TreeMap中不允许出现重复的键
Properties集合存取应用的配置项
Enumeration枚举
Collection.addAll()
Collection.reverse()
Colection.sort()
Collection.shuffle()//随机洗牌
Collection.replaceAll()
Array.sort()
Array.fill()//填充
Array.toString()//数组转化为字符串
IO流,字节InputStream OutPutStream 字符流Reader Writer
字节流读取文件
文件的拷贝
字节流缓冲区
字节缓冲流
字符流操作文件
LineNumberReader 是 BufferedReader的子类
转换流
其他IO流
ObjectInputSream ObjectOutStream
DataInPutStream DataOutStream
PrintStream
标准输入输出流
管道流PipedInputStream
ByteArrayInputStream ByteArrayOutStream
CharArrayReader CharArrayWriter
SequenceInputStream
File类
file.
遍历目录下文件
删除文件及目录
RandomAccessFile
字符编码
字符传输
边栏推荐
猜你喜欢
随机推荐
Excel动态图制作
情景剧《重走长征路》上演
3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
Jay Chou's new song is released, crawl the "Mojito" MV barrage, and see what the fans have to say!
循环结构--while循环
The sitcom "Re-Walking the Long March" was staged
ansible模块--yum模块
The 38-year-old daughter is not in love and has no stable job, the old mother is crying
38岁女儿不恋爱没有稳定工作老母亲愁哭
[Science of Terminology] For those difficult words about the integrated workbench, read this article to understand in seconds!
FPGA手撕代码——CRC校验码的多种Verilog实现方式 (2021乐鑫科技数字IC提前批代码编程)
Mysql transaction isolation level and MVCC (multi-version concurrency control)
从众多接口中脱颖而出的最稳定的接口——淘宝详情api
21天学习挑战赛--第一天打卡(屏幕密度)
Oracle根据时间查询
leetcode: 200. Number of islands
LayaBox---TypeScript---Namespaces and modules
你好,我的新名字叫“铜锁/Tongsuo”
LayaBox---TypeScript---三斜线指令
图形处理单元(GPU)的演进