Java学习 day15
大数值运算
public static void main(String[] args){
BigInteger big1 = new BigInteger("200000000");
BigInteger big2 = new BigInteger("100000000");
big1 = big1.add(big2);
big1 = big1.multiply(big2);
big1 = big1.divide(big2);
big1 = big1.subtract(big2);
BigDecimal big3 = new BigDecimal("0.001");
BigDecimal big4 = new BigDecimal("0.001");
BigDecimal big6 = new BigDecimal("10");
BigDecimal big7 = new BigDecimal("3");
big6 = big6.divide(big7, 3, BigDecimal.ROUND_HALF_UP);
System.out.println(big6);
}
枚举 enum
枚举:当某个类有几个固定的对象,可以考虑使用枚举。 例如:有一个类季节,创建四个对象(Spring Summer Autumn Winter) 使用枚举:
public enum Season{
spring("春天","万物复苏"),
summer("夏天","烈日炎炎"),
autumn("秋天","秋高气爽"),
winter("冬天","银装素裹");
private String name;
private String info;
private Season(){}
private Season(String name, String info){
this.name = name;
this.info = info;
}
@Override
public String toString(){
return name + info;
}
}
public static void main(String[] args){
Season season1 = Season.valueOf("Spring");
System.out.println(season1);
Season[] season2 = Season.values();
for (Season sea : season) {
System.out.println(sea);
}
System.out.println(Season.Spring);
}
使用枚举还可以做很多事情,组织枚举(创建一个接口,用枚举的方法在其中构建多个状态码)
初入集合 ArrayList
Java集合实际上是一种Java的类库,里面提供很多已经实现的数据结构。
初识List
List与Set:List有索引,按照放入的顺序自动生成索引下标,可重复。Set没有索引,放入的顺序与取出的顺序不一致,不可重复
public static void main(String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("123");
list.add(0, "456");
ArrayList<String> newList1 = new ArrayList<>();
Collections.addAll(newList1, "qwe", "222", "wert");
list.addAll(newList1);
ArrayList<String> newList2 = new ArrayList<>();
Collections.addAll(newList2, "xxx","yyy","zzz","xxx");
list.addAll(5, newList2);
System.out.println(list.contains("123"));
System.out.println(list.containsAll(newList1));
System.out.println(list.indexOf("123"));
System.out.println(list.lastIndexOf("xxx"));
System.out.println(list.isEmpty());
list.remove("xyz");
list.remove(0);
list.removeIf(new Predicate<String>(){
@Override
public boolean test(String t) {
int len = t.length();
if(len == 5){
return true;
}
return false;
}
});
list.removeAll(newList1);
System.out.println("获取元素个数:" + list.size());
list.set(2, "aaa");
String name = list.get(2);
list.retainAll(newList1);
String[] strs = list.toArray();
System.out.println(Arrays.toString(strs));
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
foreach(String str : list){
System.out.println(str);
}
Iterator<String> it = list.iterator();
while(it.hasNext()){
String next = it.next();
System.out.println(next);
}
ListIterator<String> it1 = list.listIterator();
while(it1.hasNext()){
String next = it1.next();
System.out.println(next);
}
}
|