存储基本数据类型
ArrayList对象不能存储基本类型,只能存储引用类型,不能写,但是存储基本类型我们可以选择它的包装类。
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
只需要注意Integer和Character
public class Demo {
public static void main(String[] args) {
ArrayList<Integer> integers=new ArrayList<>();
integers.add(1);
integers.add(3);
integers.add(4);
integers.add(5);
System.out.println(integers);
}
}
例子
数值添加到集合:
生成6个1-33之间的随机数添加到集合中,并遍历。
public class Demo {
public static void main(String[] args) {
//创建我们随机数对象
Random random=new Random();
//创建一个集合的对象
ArrayList<Integer> arrayList=new ArrayList<>();
//添加随机数到集合
for (int i = 0; i <6; i++) {
int r=random.nextInt(33)+1;
arrayList.add(r);
}
for (int i = 0; i <arrayList.size() ; i++) {
System.out.println(arrayList.get(i));
}
}
}
自定义4个学生类对象添加到集合。
public class Demo {
public static void main(String[] args) {
//创建我们集合的对象
ArrayList<Student> arrayList=new ArrayList<>();
//创建学生的对象
Student student1=new Student("哈登",30);
Student student2=new Student("威少",30);
Student student3=new Student("字母哥",25);
Student student4=new Student("肖华",50);
//将学生的对象添加到我们的集合中
arrayList.add(student1);
arrayList.add(student2);
arrayList.add(student3);
arrayList.add(student4);
//遍历一下我们的集合
for (int i = 0; i <arrayList.size(); i++) {
Student student=arrayList.get(i);
System.out.println(student.getName()+"‐‐‐‐‐"+student.getAge());
}
}
}
打印集合的方法
定义以制定的格式打印集合的方法(ArrayList作为方法的参数),使用{}括起来的集合,使用@分割每一个元素,格式参数{元素@元素@元素}。
public class Demo {
public static void main(String[] args) {
/*定义以制定的格式打印集合的方法(ArraList作为方法的参数),使用{}括起来的集合,使用@分割每一个元素,格式参数{元素@元素@元素}*/
ArrayList<String> arrayList=new ArrayList<>();
String str="哈登";
String str1="威少";
String str2="字母哥";
arrayList.add(str);
arrayList.add(str1);
arrayList.add(str2);
printArrayList(arrayList);
}
private static void printArrayList(ArrayList<String> arrayList) {
System.out.print("{");
//遍历我们的集合
for (int i = 0; i < arrayList.size(); i++) {
//获取我们的元素
String str=arrayList.get(i);
if(i!=(arrayList.size()‐1)){
System.out.print(str+"@");
}else{
System.out.print(str+"}");
}
}
}
}
定义获取所有偶数元素的集合方法(ArrayList作为我们方法的返回值)。
public class Demo {
public static void main(String[] args) {
//定义获取所有偶数元素的集合的方法(ArrayList作为我们方法的返回值)
//创建随机数的对象
Random random=new Random();
//创建我们的ArrayList集合
ArrayList<Integer> arrayList=new ArrayList<>();
//添加我们的随机数到集合中
for (int i = 0; i <30; i++) {
int r=random.nextInt(300)+1;
arrayList.add(r);
}
ArrayList<Integer> arrayList1 = getArrayList(arrayList);
System.out.println(arrayList1);
}
public static ArrayList<Integer> getArrayList(ArrayList<Integer> arrayList){
//再创建一个集合存放我们的偶数
ArrayList<Integer> arrayList1=new ArrayList<>();
//遍历我们方法传过来的集合
for (int i = 0; i <arrayList.size() ; i++) {
//判断为偶数,添加到我们的新创建的集合中
Integer num=arrayList.get(i);
if(num%2==0){
arrayList1.add(num);
}
}
return arrayList1;
}
}
标String类
概述:1、String类代表字符串。
Java程序中的所有字符串文字(例如"abd")都被视为此类的实例。字符串不变。
2、String类包括用于检查序列的各个字符的方法,用于比较字符串,搜索字符串,提取子字符串以及创建将所有字符翻译为大写或小写的字符串的副本。
特点:
1、字符串不变 。 字符串的值在创建后就不会发生变化:
String s1="abs";
s1+="sd";
System.out.println(s1);
//内存中两“abs”,“abssd”两个对象,刚开始s1指向的是我们的“abs”,拼接完以后,我们s1就指向了新的地址“abssd”。
2、因为我们String对象是不可变的,所以它可以被共享。
String s1="abc";
String s2="abc";
3、
String str="abc";
相当于:
char data[]={'a','b','c'};
String str=new String(data);
使用
1、java.lang不需要导包。
2、构造方法。
public class Demo {
public static void main(String[] args) {
//无参构造创建一个字符串对象
String str=new String();
//通过传字符数组构造字符串对象
char[] charArr={'a','b','c'};
String str1=new String(charArr);
System.out.println(str1);
//通过字节数组构造
byte[] bytes={97,98,99};
String str3=new String(bytes);
System.out.println(str3);
}
}
常用的方法:
判断功能的方法
public class Demo {
public static void main(String[] args) {
//创建字符串
String str1="hello world";
String str2="hello world";
String str3="Hello World";
//equals方法,比较两个字符串是否相同
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
//equalsIgnoreCose方法,不区分大小写的比较两个字符串是否相同
System.out.println(str1.equalsIgnoreCase(str3));
}
}
获取功能的方法
public int length()
:放回此字符串的长度。
public String concat(String str)
:将指定的字符串连接到我们该字符串的尾部。
public char charAt(int index)
:返回指定索引处的char的值。
public int indexOf(String str)
:返回指定子字符串第一次出现在该字符串中内的索引值 。
public String subString(int index)
:返回一个子字符串,从index开始截取字符串到字符串尾部。
public String subString(int begin,int end)
:返回一个子字符串,从begin开始到end结束,包含begin,但不包含end的值。
public class Demo {
public static void main(String[] args) {
//创建几个字符串
String str1="hello world";
String str2="hello world";
String str3="Hello World";
//equals方法,比较两个字符串是否相同
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
//equalsIgnoreCose方法,不区分大小写的比较两个字符串是否相同
System.out.println(str1.equalsIgnoreCase(str3));
//length(),返回字符串的长度
System.out.println(str1.length());
//concat(),将指定字符串拼接到我们该字符串的尾部
System.out.println(str1.concat("!Good"));
//charAt(),返回指定索引处的字符
System.out.println(str1.charAt(4));
//indexOf(),返回指定子字符串第一次出现在该字符串中的索引值
System.out.println(str1.indexOf(" "));
//substring(),返回从指定索引值处的字符串截取到尾部
System.out.println(str1.substring(7));
//substring(,),返回从指定索引处的字符串截取到指定的索引处(不包含最后)
System.out.println(str1.substring(3,9));
}
}
转换功能的方法
public char[] toCharArray()
:将字符串转换为新的字符数组。
public byte[] getBytes()
:使用平台默认的字符集,将该字符串编码转化为新的字节数组。
分割的方法
public String[] split(String regex)
:将次字符串按照给定的规则拆分为字符串数组。
练习
1、定义一个方法,把数组{1,2,3}按照指定的格式拼接成一个字符串。格式“【元素#元素#元素】
public class Demo {
public static void main(String[] args) {
//把数组{1,2,3}按照指定的格式拼接成一个字符串:格式:【元素#元素#元素】
int[] arr={1,2,3};
String string = arrayToString(arr);
System.out.println(string);
}
//返回值是一个字符串String
//参数类型:int类型数据
private static String arrayToString(int[] arr) {
//创建一个开始的字符串
String str="【";
//遍历数组拼接
for (int i = 0; i <arr.length ; i++) {
if(i==arr.length‐1){
str=str.concat(arr[i]+"】");
}else{
str=str.concat(arr[i]+"#");
}
}
return str;
}
}
2、键盘录入一个字符串,统计字符串中大小写字母的个数以及数字字符的个数。
public class Demo {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
//键盘录入字符串
System.out.println("请输入一个字符串:");
String str=scanner.nextLine();
int bigCount=0;
int smallCount=0;
int numberCount=0;
//遍历我们的字符串
for (int i = 0; i <str.length() ; i++) {
char ch=str.charAt(i);
if(ch>='A'&&ch<='Z'){
bigCount++;
}else if(ch>='a'&&ch<='z') {
smallCount++;
}else if(ch>='0'&&ch<='9'){
numberCount++;
}else{
System.out.println("该字符是一个非法字符!"+ch);
}
}
System.out.println("大写字符:"+bigCount);
System.out.println("小写字符:"+smallCount);
System.out.println("数字字符:"+numberCount);
}
}
static关键字
概述:关于我们static关键字的使用,它可以修饰我们成员变量和成员方法,被修饰的成员属于类,而不是属于单单一个类的,既然static这个修饰的东西属于类,那么调用的时候,就不需要创建对象去调用。
类变量:当static修饰我们的成员变量的时候,该变量属于类,该类中每个对象都共享同一个类的变量的值,任何对象可以更改我们类变量的值,但是可以在不创建对象的情况对类变量进行操作。
类变量
public class Student {
//学生的姓名
private String name;
//学生的年龄
private int age;
//学生的学号
private int sid;
//类变量,记录学生的数量,分配学号
public static int numberStudent=0;
public Student(String name, int age) {
this.name = name;
this.age = age;
this.sid = ++numberStudent;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sid=" + sid +
'}';
}
}
public class Demo {
public static void main(String[] args) {
Student student=new Student("哈登",30);
Student student1=new Student("威少",29);
Student student2=new Student("字母哥",30);
System.out.println(student.toString());
System.out.println(student1.toString());
System.out.println(student2.toString());
//类变量的调用:类名.类变量
System.out.println(Student.numberStudent);
}
}
静态方法
当static修饰我们的成员方法的时候,该方法及称为类方法(静态方法),静态方法在申明的时候用static,建议使用的时候,直接用我们的类名.方法名。
调用我们的类方法,直接调用:
类名.方法名();
注意事项
1、静态方法可以访问我们的静态变量和静态方法。
2、静态方法不能直接访问我们的成员变量和成员方法。反之,成员方法可以直接访问我们的静态变量和静态方法。
3、静态方法不能使用this关键字。
总之一句话:静态的东西是随着类的加载而加载,优于对象存在。静态访问只能访问静态成员。
图解析:
1、随着类的加载而加载,且只加载一次。
2、存储在我们固定取余(静态区),可以直接使用类名调用。
3、它优于对象存在,所以,所有的对象共享。
静态代码块
静态代码块:定义在我们成员变量的位置,使用static修饰。
格式:
static{
}
位置:类中方法外。
执行:随着类的加载而执行一次且只有一次,优于我们main方法和我们的构造方法执行。
public class Game {
//类变量
public static int number;
public static ArrayList<String> list;
//初始化
static {
//给类变量赋值
number=2;
list=new ArrayList<>();
list.add("abc");
list.add("小白");
}
public static void main(String[] args) {
System.out.println(Game.number);
}
}
static关键字,可以修饰变量、方法和代码块,在使用过程中,其中目的就是不创建对象去直接调用。后面讲两个工具类:Math类、Arrays类。