java基础篇
IDEA常用快捷键
快捷键功能
Alt+Enter自动修正代码Ctrl+Y删除光标所在行Ctrl+D复制光标所在行的内容,插入光标位置下面Ctrl+Alt +L格式化代码Ctrl+/单行注释,再按取消注释Ctrl+Shift+/选中代码注释,多行注释,再按取消注释。Alt+Ins自动生成代码,toString,get,set等方法Alt+Shift+上下箭头移动当前代码行
ArrayList集合概述
基本使用
package cn
.itcast
.day01
.demo01
.array
;
import java
.util
.ArrayList
;
public class Demo02ArrayList {
public static void main(String
[] args
) {
ArrayList
<String> list
= new ArrayList<>();
System
.out
.println(list
);
list
.add("刘诗诗");
list
.add("唐嫣");
list
.add("杨幂");
list
.add("迪丽热巴");
System
.out
.println(list
);
}
}
ArrayList常用方法和遍历
package cn
.itcast
.day01
.demo01
.array
;
import java
.util
.ArrayList
;
public class Demo03ArrayListMethod {
public static void main(String
[] args
) {
ArrayList
<String> list
= new ArrayList<>();
System
.out
.println(list
);
boolean success
= list
.add("刘诗诗");
list
.add("唐嫣");
list
.add("吕一");
list
.add("舒畅");
System
.out
.println(list
);
System
.out
.println("添加的动作是否成功:" + success
);
String name
= list
.get(2);
System
.out
.println("第2号索引的位置:" + name
);
String whoRemove
= list
.remove(2);
System
.out
.println(list
);
int size
= list
.size();
System
.out
.println("集合的长度是:" + size
);
}
}
package cn
.itcast
.day01
.demo01
.array
;
import java
.util
.ArrayList
;
public class Demo04ArrayListEach {
public static void main(String
[] args
) {
ArrayList
<String> list
= new ArrayList<>();
list
.add("迪丽热巴");
list
.add("杨幂");
list
.add("唐嫣");
list
.add("刘诗诗");
for (int i
= 0; i
< list
.size(); i
++) {
System
.out
.println(list
.get(i
));
}
}
}
ArrayList集合存储基本数据类型
package cn
.itcast
.day01
.demo01
.array
;
import java
.util
.ArrayList
;
public class Demo05ArrayListBasic {
public static void main(String
[] args
) {
ArrayList
<String> listA
= new ArrayList<>();
ArrayList
<Integer> listC
= new ArrayList<>();
listC
.add(100);
listC
.add(200);
listC
.add(300);
System
.out
.println(listC
);
Integer num
= listC
.get(1);
System
.out
.println("第一号元素是:" + num
);
}
}
ArrayList练习
存储随机数字
package cn
.itcast
.day01
.demo01
.array
;
import java
.util
.ArrayList
;
import java
.util
.Random
;
public class Demo01ArrayListRandom {
public static void main(String
[] args
) {
ArrayList
<Integer> list
= new ArrayList<>();
Random r
= new Random();
for (int i
= 0; i
< 6; i
++) {
int num
= r
.nextInt(33) + 1;
list
.add(num
);
}
for (int i
= 0; i
< list
.size(); i
++) {
System
.out
.println(list
.get(i
));
}
}
}
存储自定义对象
package cn
.itcast
.day01
.demo01
.array
;
public class Student {
private String name
;
private int age
;
public Student() {
}
public Student(String name
, int age
) {
this.name
= name
;
this.age
= age
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public int getAge() {
return age
;
}
public void setAge(int age
) {
this.age
= age
;
}
}
package cn
.itcast
.day01
.demo01
.array
;
import java
.util
.ArrayList
;
public class Demo02ArrayListStudent {
public static void main(String
[] args
) {
ArrayList
<Student> list
= new ArrayList<>();
Student one
= new Student("洪七公", 20);
Student two
= new Student("段誉", 30);
Student three
= new Student("黄药师", 40);
Student four
= new Student("南帝", 50);
list
.add(one
);
list
.add(two
);
list
.add(three
);
list
.add(four
);
for (int i
= 0; i
< list
.size(); i
++) {
Student stu
= list
.get(i
);
System
.out
.println("姓名:" + stu
.getName() + ",年龄" + stu
.getAge());
}
}
}
按指定格式遍历
package cn
.itcast
.day01
.demo01
.array
;
import java
.util
.ArrayList
;
public class Demo03ArrayListPrint {
public static void main(String
[] args
) {
ArrayList
<String> list
= new ArrayList<>();
list
.add("张三丰");
list
.add("张四丰");
list
.add("张五丰");
list
.add("张六丰");
System
.out
.println(list
);
printArrayList(list
);
}
public static void printArrayList(ArrayList
<String> list
) {
System
.out
.print("{");
for (int i
= 0; i
< list
.size(); i
++) {
String name
= list
.get(i
);
if(i
== list
.size() -1) {
System
.out
.print(name
+ "}");
}else{
System
.out
.print(name
+ "@");
}
}
}
}
筛选集合中的随机
package cn
.itcast
.day01
.demo01
.array
;
import java
.util
.ArrayList
;
import java
.util
.Random
;
public class Demo04ArrayListReturn {
public static void main(String
[] args
) {
ArrayList
<Integer> bigList
= new ArrayList<>();
Random r
= new Random();
for (int i
= 0; i
< 20; i
++) {
int num
= r
.nextInt(100) + 1;
bigList
.add(num
);
}
ArrayList
<Integer> smallList
= getSmallList(bigList
);
System
.out
.println("偶数总共有多少个:" + smallList
.size());
for (int i
= 0; i
< smallList
.size(); i
++) {
System
.out
.println(smallList
.get(i
));
}
}
public static ArrayList
<Integer> getSmallList(ArrayList
<Integer> bigList
) {
ArrayList
<Integer> smallList
= new ArrayList<>();
for (int i
= 0; i
< bigList
.size(); i
++) {
Integer num
= bigList
.get(i
);
if(num
% 2 == 0) {
smallList
.add(num
);
}
}
return smallList
;
}
}
转载请注明原文地址:https://tech.qufami.com/read-10222.html