基本 类型与字符串类型之间的相互转换
基本类型->字符串(String)
1,基本类型的值 + “”最简单的方法(工作中常用)
2,包装类的静态方法toString (参数),不是Object类的toString()重载
static String toString (int i)返回一个表示指定整数的 String 对象。
3. String类的静态方法valueOf(参数)
static String valueOf(int i)返回 int参数的字符串表示形式
字符串(String)->基本类型
使用包装类的静态方法parseXXX(“字符串”);
Integer类:static int parseInt(String s)
Double类: static double parseDouble(String s)
public class DemoInteger {
public static void main(String[] args){
//基本类性 ->字符串(String)
int i1 = 100;
String s1 = i1 + "";
System.out.println(s1 + 200);//100200
String s2 = Integer.toString(100);
System.out.println(s2 + 200);//100200
String s3 = String.valueOf(100);
System.out.println(s3 + 200);//1002000
System.out.println("==============");
//字符串(String) -> 基本类型
int i = Integer.parseInt(s1);
System.out.println(i - 10);//90
// int a = Integer.parseInt("a");//NumberFormatException:
// System.out.println(a);
//
}
}
Collection集合
集合:集合是java中提供的一种容器,可以用来存储多个数据
集合框架:
javaSE提供满足各种需求的API,在使用这些API 前,先了解其继承与接口操作架构,才能了解何时采用哪个类,以及类之间如何批次合作,从而达到灵活应用。
集合按照其存储结构分为两大类:分别是单列集合java.util.Collection和双列集合java.util.Map.
Collection集合常用功能
java.ytil.Collection接口
所有单列集合的最顶层的接口,里面定义类所有单列集合共性的方法
任意的单列集合都可以使用Collection接口的方法
共性的 方法
public boolean add(E e):把给定的对象添加到当前集合中。
public void clear():清空集合中所有元素。「但是不删除集合,集合还存在 」
public boolean remove (E e):把给定的对象在当前集合中删除
public boolean contains(E e):判断当前集合中是否包含给定的对象。
public boolean isEmpty();判断当前集合是否为空
public int size();返回集合中元素的个数
public Object[]toArray();把集合中的元素,存储到数组中。
import java.util.Collection;
import java.util.ArrayList;
import java.util.HashSet;
public class DemoCollection {
public static void main(String[] args){
//创建集合对象,可以使用多态
// Collection<String> coll = new ArrayList<String>();、、有序集合
Collection<String> coll = new HashSet<String>();//无序集合,「不允许存储重复元素,
//放俩李四,输出以后就一个」。
System.out.println();//重写类toString方法[]
// public boolean add(E e):把给定的对象添加到当前集合中。
//返回值是一个boolean值,一般都返回true,所以可以不用接收
boolean b1 = coll.add("张三");
System.out.println("b1" + b1);//b1:true
System.out.println(coll);//[张三]
coll.add("李四");
coll.add("李四");
coll.add("王五");
coll.add("赵六");
System.out.println(coll);//[张三,李四,王五,赵六]
//public boolean remove (E e):把给定的对象在当前集合中删除(删除指定的元素)
//返回值是一个boolean值,集合中存在元素,删除元素,返回true
// 集合不存在元素,删除失败,返回false
boolean b2 = coll.remove("赵六");
System.out.println("b2" + b2 );//b2 :true
boolean b3 = coll.remove("赵四");
System.out.println("b3" + b3);//b3: false 也就是没有这个赵四人删除失败
System.out.println(coll);//[张三,李四,王五]
// public boolean contains(E e):判断当前集合中是否包含给定的对象。
//包含返回true
//不包含返回false
boolean b4 = coll.contains("李四");
System.out.println("b4" + b4); //b4true
boolean b5 = coll.contains("赵四");
System.out.println("b5" + b5);//b5false
// public boolean isEmpty();判断当前集合是否为空.集合为空返回true,集合不为空返回false
boolean b6 = coll.isEmpty();
System.out.println("b6:" + b6);//b6 false
// public int size();返回集合中元素的个数
int s = coll.size();
System.out.println(s);//3
//public Object[]toArray();把集合中的元素,存储到数组中。
Object[] arr = coll.toArray();
for(int i = 0; i < arr.length; i++){
System.out.println(arr[i]);
}
// public void clear():清空集合中所有元素。 但是不删除集合,集合还存在
coll.clear();
System.out.println(coll);//[]
}
}