一、 ArrayList集合概述和基本使用
package cn
.itcast
.day07
.demo04
;
import java
.util
.ArrayList
;
public class Demo02ArrayList {
public static void main(String
[] args
) {
ArrayList
<String> list
= new ArrayList<>();
System
.out
.println(list
);
list
.add("赵丽颖");
System
.out
.println(list
);
list
.add("迪丽热巴");
list
.add("古力娜扎");
list
.add("玛尔扎哈");
System
.out
.println(list
);
}
}
二、ArrayList集合的常用方法和遍历
package cn
.itcast
.day07
.demo04
;
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("柳岩");
System
.out
.println(list
);
System
.out
.println("添加的动作是否成功:" + success
);
list
.add("高圆圆");
list
.add("赵又廷");
list
.add("李小璐");
list
.add("贾乃亮");
System
.out
.println(list
);
String name
= list
.get(2);
System
.out
.println("第2号索引位置:" + name
);
String whoRemoved
= list
.remove(3);
System
.out
.println("被删除的人是:" + whoRemoved
);
System
.out
.println(list
);
}
}
package cn
.itcast
.day07
.demo04
;
import java
.util
.ArrayList
;
public class Demo04ArrayListEach {
public static void main(String
[] args
) {
ArrayList
<String
> list
= new ArrayList<>();
list
.add("迪丽热巴");
list
.add("古力娜扎");
list
.add("玛尔扎哈");
for (int i
= 0; i
< list
.size(); i
++) {
System
.out
.println(list
.get(i
));
}
}
}
三、ArrayList集合存储基本数据类型
package cn
.itcast
.day07
.demo04
;
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);
System
.out
.println(listC
);
int num
= listC
.get(1);
System
.out
.println("第1号元素是:" + num
);
}
}
四、ArrayList练习一_存储随机数字
package cn
.itcast
.day07
.demo05
;
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
));
}
}
}
五、ArrayList练习二_存储自定义对象
package cn
.itcast
.day07
.demo05
;
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("欧阳锋", 21);
Student three
= new Student("黄药师", 22);
Student four
= new Student("段智兴", 23);
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
.day07
.demo05
;
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
;
}
}
六、ArrayList练习三_按指定格式遍历集合字符串
package cn
.itcast
.day07
.demo05
;
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
.println(name
+ "}");
} else {
System
.out
.print(name
+ "@");
}
}
}
}
七、ArrayList练习四_筛选集合中的随机数
package cn
.itcast
.day07
.demo05
;
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> list
= smallList(bigList
);
System
.out
.println("偶数个数为:" + list
.size());
for (int i
= 0; i
< list
.size(); i
++) {
System
.out
.println(list
.get(i
));
}
}
public static ArrayList
<Integer> smallList(ArrayList
<Integer> bigList
) {
ArrayList
<Integer> smallList
= new ArrayList<>();
for (int i
= 0; i
< bigList
.size(); i
++) {
int num
= bigList
.get(i
);
if (num
% 2 == 0) {
smallList
.add(num
);
}
}
return smallList
;
}
}
八、字符串概述和特点
package cn
.itcast
.day08
.demo01
;
public class Demo01String {
}
九、字符串的构造方法和直接创建
package cn
.itcast
.day08
.demo01
;
public class Demo01String {
public static void main(String
[] args
) {
String str1
= new String();
System
.out
.println("第1个字符串:" + str1
);
char[] charArray
= {'A', 'B', 'C'};
String str2
= new String(charArray
);
System
.out
.println("第2个字符串:" + str2
);
byte[] byteArray
= {97, 98, 99};
String str3
= new String(byteArray
);
System
.out
.println("第3个字符串:" + str3
);
String str4
= "Hello";
System
.out
.println("第4个字符串:" + str4
);
}
}
十、字符串的常量池
package cn
.itcast
.day08
.demo01
;
import javax
.xml
.transform
.sax
.SAXResult
;
public class Demo02StringPool {
public static void main(String
[] args
) {
String str1
= "abc";
String str2
= "abc";
char[] charArray
= {'a', 'b', 'c'};
String str3
= new String(charArray
);
System
.out
.println(str1
== str2
);
System
.out
.println(str1
== str3
);
System
.out
.println(str2
== str3
);
}
}
十一、字符串的比较相关方法
package cn
.itcast
.day08
.demo02
;
public class Demo01StringEquals {
public static void main(String
[] args
) {
String str1
= "Hello";
String str2
= "Hello";
char[] charArray
= {'H', 'e', 'l', 'l', 'o'};
String str3
= new String(charArray
);
System
.out
.println(str1
.equals(str2
));
System
.out
.println(str2
.equals(str3
));
System
.out
.println(str3
.equals("Hello"));
System
.out
.println("Hello".equals(str1
));
String str4
= "hello";
System
.out
.println(str1
.equals(str4
));
System
.out
.println("==========================");
String str5
= null
;
System
.out
.println("abc".equals(str5
));
System
.out
.println("===========================");
String strA
= "Java";
String strB
= "java";
System
.out
.println(strA
.equals(strB
));
System
.out
.println(strA
.equalsIgnoreCase(strB
));
System
.out
.println("abc一123".equalsIgnoreCase("abc壹123"));
}
}
十二、字符串的获取相关方法
package cn
.itcast
.day08
.demo02
;
public class Demo02StringGet {
public static void main(String
[] args
) {
int length
= "afjafnakjgioajgoa".length();
System
.out
.println("字符串的长度是:" + length
);
String str1
= "Hello";
String str2
= "World";
String str3
= str1
.concat(str2
);
System
.out
.println(str1
);
System
.out
.println(str2
);
System
.out
.println(str3
);
System
.out
.println("=====================");
char ch
= "Hello".charAt(1);
System
.out
.println("在1号索引位置的字符是:" + ch
);
System
.out
.println("======================");
String original
= "HelloWorldHelloWorld";
int index
= original
.indexOf("oll");
System
.out
.println("第一次索引值是:" + index
);
}
}
十三、字符串的截取方法
package cn
.itcast
.day08
.demo02
;
public class Demo03Substring {
public static void main(String
[] args
) {
String str1
= "HelloWorld";
String str2
= str1
.substring(5);
System
.out
.println(str1
);
System
.out
.println(str2
);
System
.out
.println("=========================");
String str3
= str1
.substring(4, 7);
System
.out
.println(str3
);
System
.out
.println("=========================");
String strA
= "Hello";
System
.out
.println(strA
);
strA
= "Java";
System
.out
.println(strA
);
}
}
十四、字符串的转换相关方法
package cn
.itcast
.day08
.demo02
;
public class Demo04StringConvert {
public static void main(String
[] args
) {
char[] chars
= "Hello".toCharArray();
System
.out
.println(chars
[0]);
System
.out
.println(chars
.length
);
System
.out
.println("=====================");
byte[] bytes
= "abc".getBytes();
for (int i
= 0; i
< bytes
.length
; i
++) {
System
.out
.println(bytes
[i
]);
}
System
.out
.println("======================");
String str1
= "How do you do?";
String str2
= str1
.replace("o", "*");
System
.out
.println(str1
);
System
.out
.println(str2
);
System
.out
.println("=======================");
String lang1
= "会不会玩儿呀!你大爷的!你大爷的!你大爷的!你大爷的!";
String lang2
= lang1
.replace("你大爷的", "*****");
System
.out
.println(lang2
);
}
}
十五、字符串的分割方法
package cn
.itcast
.day08
.demo02
;
public class Demo05StringSplit {
public static void main(String
[] args
) {
String str1
= "aaa,bbb,ccc";
String
[] array1
= str1
.split(",");
for (int i
= 0; i
< array1
.length
; i
++) {
System
.out
.println(array1
[i
]);
}
System
.out
.println("=======================");
String str2
= "aaa bbb ccc";
String
[] array2
= str2
.split(" ");
for (int i
= 0; i
< array2
.length
; i
++) {
System
.out
.println(array2
[i
]);
}
System
.out
.println("=======================");
String str3
= "aaa.bbb.ccc";
String
[] array3
= str3
.split("\\.");
for (int i
= 0; i
< array3
.length
; i
++) {
System
.out
.println(array3
[i
]);
}
}
}
十六、练习:按指定格式拼接字符串
package cn
.itcast
.day08
.demo02
;
public class Demo06StringPractise {
public static void main(String
[] args
) {
int[] array
= {1, 2, 3};
String result
= fromArrayToString(array
);
System
.out
.println(result
);
}
public static String
fromArrayToString(int[] array
) {
String str
= "[";
for (int i
= 0; i
< array
.length
; i
++) {
if (i
== array
.length
- 1) {
str
+= "word" + array
[i
] + "]";
} else {
str
+= "word" + array
[i
] + "#";
}
}
return str
;
}
}
十七、练习:统计输入的字符串中各种字符的个数
package cn
.itcast
.day08
.demo02
;
import java
.util
.Scanner
;
public class Demo07StringCount {
public static void main(String
[] args
) {
Scanner sc
= new Scanner(System
.in
);
System
.out
.println("请输入一个字符串:");
String input
= sc
.next();
int countUpper
= 0;
int countLower
= 0;
int countNumber
= 0;
int countOther
= 0;
char[] charArray
= input
.toCharArray();
for (int i
= 0; i
< charArray
.length
; i
++) {
char ch
= charArray
[i
];
if ('A' <= ch
&& ch
<= 'Z') {
countUpper
++;
} else if ('a' <= ch
&& ch
<= 'z' ) {
countLower
++;
} else if ('0' <= ch
&& ch
<= '9') {
countNumber
++;
} else {
countOther
++;
}
}
System
.out
.println("大写字母有:" + countUpper
);
System
.out
.println("小写字母有:" + countLower
);
System
.out
.println("数字有:" +countNumber
);
System
.out
.println("其他字符有:" + countOther
);
}
}
十八、静态Static关键字概述
十九、静态Static关键字修饰成员变量
package cn
.itcast
.day08
.demo03
;
public class Student {
private int id
;
private String name
;
private int age
;
static String room
;
private static int idCounter
= 0;
public Student() {
idCounter
++;
}
public Student(String name
, int age
) {
this.name
= name
;
this.age
= age
;
this.id
= ++idCounter
;
}
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
;
}
public int getId() {
return id
;
}
public void setId(int id
) {
this.id
= id
;
}
}
package cn
.itcast
.day08
.demo03
;
public class Demo01StaticField {
public static void main(String
[] args
) {
Student one
= new Student("郭靖", 19);
one
.room
= "101教室";
System
.out
.println("姓名:" + one
.getName() +
",年龄:" + one
.getAge() + ",教室" +
one
.room
+ ",学号:" + one
.getId());
Student two
= new Student("黄蓉", 16);
System
.out
.println("姓名:" + two
.getName() +
",年龄:" + two
.getAge() + ",教室" +
two
.room
+ ",学号:" + two
.getId());
}
}
二十、静态Static关键字修饰成员方法
package cn
.itcast
.day08
.demo03
;
public class MyClass {
int num
;
static int numStatic
;
public void method() {
System
.out
.println("这是一个成员方法。");
System
.out
.println(num
);
System
.out
.println(numStatic
);
}
public static void methodStatic() {
System
.out
.println("这是一个镜头方法");
System
.out
.println(numStatic
);
}
}
package cn
.itcast
.day08
.demo03
;
public class Demo02StaticMethod {
public static void main(String
[] args
) {
MyClass obj
= new MyClass();
obj
.method();
obj
.methodStatic();
MyClass
.methodStatic();
myMethod();
Demo02StaticMethod
.myMethod();
}
public static void myMethod() {
System
.out
.println("自己方法!");
}
}
转载请注明原文地址:https://tech.qufami.com/read-16841.html