java基础类型用法
2021-01-29 本文已影响0人
wyc0859
image.png
ArrayList
public void lists() {
List list = new ArrayList();
list.add("demo1");
list.add("demo2");
list.add("demo1");
list.add("demo2");
int i = list.indexOf("demo2");
System.out.println("list集合中所有的元素: " + list);
list.forEach((e) -> {
System.out.print(e + ", ");
});
System.out.println("************************");
//toArray() 将 arraylist 转换为数组。
Object[] obj = list.toArray();
for(Object item : obj) {
System.out.print(item+", ");
}
System.out.println("************************");
System.out.println("第一-次出现demo2的索引位置是: " + i);
i = list.indexOf("demo3" );
System.out.println( "第一-次出现demo3的索引位置是: " + i);
//下标获取值
System.out.println("index1:"+list.get(1));
//删除指定索引的对象
list.remove(2);
System.out.println("删除后的list: " + list);
//在指定位置插入元素,后面的元素都往后移一个元素。
list.add(1,"demo5");
System.out.println("插入元素后的list: " + list);
//在索引为index位置的元素替换为element元素
list.set(2,"demo3");
System.out.println("插入元素后的list: " + list);
//返回从索引fromIndex到toIndex的元素集合,包左不包右
List list2 = new ArrayList();
list2=list.subList(1,3);
System.out.println("list22222: " + list2);
}
List循环下标的2种方式
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
Stream.iterate(0, i -> i + 1).limit(list.size()).forEach(i -> {
System.out.println(list.get(i));
});
IntStream.range(0,list.size()).forEach(i->{
System.out.println(list.get(i));
});
Map HashMap
public void maps(){
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// 添加键值对
Sites.put(1, "Google");
Sites.put(2, "Runoob");
Sites.put(3, "Taobao");
Sites.put(4, "Zhihu");
System.out.println(Sites);
System.out.println(Sites.get(2));
Sites.remove(3);
System.out.println(Sites);
// 输出 key 和 value
for (Integer i : Sites.keySet()) {
System.out.println("key: " + i + " value: " + Sites.get(i));
}
}
public void a1(){
Map<String,Object> map = new HashMap<>();
map.put("a","尺寸");
map.put("b","7英寸");
map.put("c",2);
map.put("d",5);
System.out.println(map);//{a=尺寸, b=7英寸, c=2, d=5}
System.out.println(map.get("b"));//7英寸
System.out.println(map.get(1)); //null
map.forEach((key, value) -> {
System.out.print(key + "=" + value + " ");
});
}
迭代器iterator
public void Iterator(){
// 创建集合
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Zhihu");
sites.add("aaa");
sites.add("bbbbbb");
// 获取迭代器
Iterator<String> it = sites.iterator();
// 输出集合中的第一个元素
System.out.println(it.next());
System.out.println(it.next());
while(it.hasNext()) {
String i = it.next();
if(i.length() < 4) {
it.remove(); // 删除小于 10 的元素
}
}
//[Google, Runoob, Taobao, Zhihu, bbbbbb]
System.out.println(sites);
}
泛型
@Test
public void tclass(){
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
String[] strArray = { "Haa", "Ebb", "L", "L", "O" };
System.out.print("int:");
for(int x : intArray ){
System.out.print( x + ",");
}
System.out.print("double:");
for(double x : doubleArray){
System.out.print( x + ",");
}
System.out.print("string:");
for(String x : strArray){
System.out.print( x + ",");
}
List<String> name = new ArrayList<String>();
List<Integer> age = new ArrayList<Integer>();
List<Number> number = new ArrayList<Number>();
name.add("icon");
age.add(18);
number.add(314);
//getUperNumber(name);//1
getUperNumber(age);//2
getUperNumber(number);//3
}
//Integer继承与Number
public static void getUperNumber(List<? extends Number> data) {
System.out.println("data :" + data.get(0));
}
}
多维map
@Test
public void a2(){
Map<String,Object> map1 = new HashMap<>();
map1.put("a","尺寸");
map1.put("b","7英寸");
map1.put("c",2);
map1.put("d",5);
Map<String,Object> map2 = new HashMap<>();
map2.put("a","1尺寸");
map2.put("b","3英寸");
map2.put("c",6);
map2.put("d",7);
System.out.println(map1);
System.out.println(map2);
System.out.println("map2:" + map2.get("b"));
//Map<String,Object>> map = new HashMap<>();
//如果如上这样定义,下面是不会有foreach和get等用法的
Map<String,Map<String,Object>> map = new HashMap<>();
map.put("0",map1);
map.put("1",map2);
System.out.println(map);
map.forEach((k,v)->{
System.out.print(k + "=" + v + " ");
System.out.println("v:" + v.get("b"));
});
}
@Test
public void aa() {
String[] runoobs = new String[3];
runoobs[0] = "菜鸟教程";
runoobs[1] = "菜鸟工具";
runoobs[2] = "菜鸟笔记";
//runoobs[3] = "菜鸟笔记";//会报错
for (String str:runoobs){
System.out.println(str+"\n");
}
Map<String,Object>[] map = new HashMap[3];
map[0] = new HashMap<String,Object>();
map[0].put("a","尺寸");
map[0].put("b","7英寸");
map[0].put("c",2);
map[1] = new HashMap<String,Object>();
map[1].put("a","5尺寸");
map[1].put("b","12英寸");
map[1].put("c",7);
for (Map<String,Object> obj:map){
obj.forEach((k,v)->{
System.out.print(k + "=" + v + " ");//能打印出来,但也会报错
//System.out.println("v:" + v.get("b")); //不存在get
});
}
}