今天是真的累,也是回学校第五天了,希望一切度好好的。
ArrayList(对象数组)
特点:ArrayList是大小可变的数组实现,存储在内的数据称之为元素。
ArrayList使用步骤
基本格式:ArrayList list=new ArrayList();
对象名.add(参数) :将指定的元素添加到此集合的尾部。
对象名.remove(int index) :移除此集合中指定位置上的元素。返回被删除的元素。
对象名.get(int index) :返回此集合中指定位置上的元素。返回获取的元素。
对象名.size() :返回此集合中的元素数。遍历集合时,可以控制索引范围,防止越界。
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(name);
String remove = list.remove(3);
System.out.println(remove);
System.out.println(list);
int size = list.size();
System.out.println(size);
}
}
ArrayList类可以作为参数和返回值进行传递
public static void printArrayList(ArrayList<String> list){
System.out.print("{");
for (int i = 0; i < list.size(); i++) {
if(i<list.size()-1)
System.out.print(list.get(i)+"@");
else
System.out.print(list.get(i));
}
System.out.println("}");
}
}
======================================================================
public static ArrayList<Integer> Screen(ArrayList<Integer> list)
{
for (int i = 0; i < list.size(); i++) {
if(list.get(i)%2!=0)
list.remove(i);
}
return list;
}
}
String类
特点:
- 字符串不变:字符串的值在创建后不会改变;
- 因为String对象是不可改变的,所以他们可以被分享;
- “abc"等效于char[] data={‘a’,‘b’,‘c’}
构造方法
1.public String():初始化新创建的String对象,以使其表示空字符序列;
2.public String(char[] value):通过当前参数中的字符数组来构造新的String;
3.public String(byte[] butes):通过平台默认的字节数组来构造新的String;
//无参构造
String str = new String();
// 通过字符数组构造 char chars[] = {'a', 'b', 'c'};
String str2 = new String(chars);
// 通过字节数组构造
byte bytes[] = { 97, 98, 99 };
String str3 = new String(bytes)
对象名.equals (Object anObject) :将此字符串与指定对象进行比较。
对象名.equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。
对象名.length () :返回此字符串的长度。
对象名.concat (String str) :将指定的字符串连接到该字符串的末尾。
对象名.charAt (int index) :返回指定索引处的 char值。
对象名.indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
对象名.substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符 串结尾。
对象名.substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。
public class Dem02StringGet {
public static void main(String[] args) {
int length = "sdasfasfascasfafasdf".length();
System.out.println(length);
Student stu = new Student();
String str1 = "hello";
String str3 = "defaxas".concat(stu.name);
System.out.println(str3);
System.out.println("hello".charAt(1));
System.out.println("dasdasfasdasfas".indexOf("fas"));
}
}
public class Demo01StringEquals {
public static void main(String[] args) {
Student stu = new Student();
String str1 = "abc";
String str2 = "abc";
char[] charArray = {'a', 'b', 'c'};
String str3 = new String(charArray);
byte[] byteArray = {97, 98, 99};
String str4 = new String(byteArray);
System.out.println(str3.equals(str4));
System.out.println("=======================");
String strA = "abcd";
String strB = "ABCD";
System.out.println(strA.equalsIgnoreCase(strB));
System.out.println(strA.equals(stu.name));
}
}
public class Demo03SubString {
public static void main(String[] args) {
System.out.println("helloworld".substring(5));;
System.out.println("helloworldsfasdasfas".substring(1,7));
}
}
对象名.toCharArray () :将此字符串转换为新的字符数组。
对象名.getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
对象名.replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使 用replacement字符串替换。
public class Demo04StringConvert {
public static void main(String[] args) {
char[] charArray = "hello".toCharArray();
System.out.println(charArray[0]);
byte[] bytes = "dsafasf".getBytes();
System.out.println(bytes[0]);
String replace = "how do you do".replace("you", "my");
System.out.println(replace);
String lang1 = "会不会玩吗?你大爷的!";
String replace1 = lang1.replace("你大爷的", "****");
System.out.println(replace1);
}
}
static关键字
特点:使用static修饰成员变量时,该类的每个对象都共享同一个类变量的值。任何对象都可以更改该类变量的值,但也可以在不创建该类的对象的情况下对类变量进行修改。
定义格式:
static 数据类型 变量名;
调用格式:
//调用静态变量
类名.类变量名;
// 调用静态方法
类名.静态方法名(参数)
注意事项:
静态方法调用的注意事项: 静态方法可以直接访问类变量和静态方法。
静态方法不能直接访问普通成员变量或成员方法。反之,成员方法可以直接访问类变量或静态方法。
静态方法中,不能使用this关键字。
public class Demo03StaticStudent {
public static void main(String[] args) {
Student.room = "101教室";
Student one = new Student("黄蓉", 21);
Student two = new Student("大家说的", 312);
System.out.println(one.getAge());
System.out.println(one.getName());
System.out.println(Student.room);
Student.room = "102教室";
System.out.println(two.getAge());
System.out.println(two.getName());
System.out.println(Student.room);
}
}
Arrays类
把数组转化为字符串
Arrays.toString(char[] str); //任何数组都可以转换为字符串
将数组进行升序排序
Arrays.sort(char[] str); //将数组进行升序排列
public class Demo02ArrayPractise {
public static void main(String[] args) {
String str = "fasfasfasvgasva2e12far4124";
char[] chars = str.toCharArray();
Arrays.sort(chars);
for (int i = chars.length - 1; i >= 0; i--) {
System.out.print(chars[i]);
}
}
}
Math工具类(了解)
public class Demo03Math {
public static void main(String[] args) {
System.out.println(Math.abs(-3.14)); //绝对值的求法
System.out.println(Math.abs(0));
System.out.println(Math.ceil(-1.2)); //上取整
System.out.println(Math.floor(-8.8)); //下取整
System.out.println(Math.round(5.42)); //四舍五入
System.out.println(Math.round(5.52));
System.out.println(Math.round(10.5));
System.out.println(Math.PI); //PI值
}
}
继承
概念:多个类中存在相同属性和行为时,将这些内容抽取到单独的一个类中,那么无需再定义这些属性和行为,只要京城一个类就行。
格式:
class 父类 { ... }
class 子类 extends 父类 { ... }
注意事项:
- 如果子类和父类中出现重名的成员变量,此时访问是有影响的
- 子父类中出现了同名的成员变量时,在子类中需要访问父类中非私有成员变量时,需要super关键字,修饰父类成员变量。
public class Fu { //父类
int num=10;
}
public class Zi extends Fu { //子类
int num=20;
public void method(){
int num=30;
System.out.println(num);
System.out.println(this.num);
System.out.println(super.num); //父类成员变量调用时需要用到super关键字
}
}
方法名重写
方法重写:子类中出现与父类一模一样的方法时(返回自类型,方法名和参数列表都一样),会出现覆盖效果,也称为重写或或者复写。声明不变,重写实现。
public class Phone { //父类
public void call(){
System.out.println("打电话");
}
public void send(){
System.out.println("发短信");
}
public void show(){
System.out.println("显示号码");
}
}
public class NewPhone extends Phone{ //子类
@Override //重写的安全检测
public void show() {
super.show();//将父类的方法拿过来继续用
System.out.println("显示头像");
System.out.println("显示姓名");
}
}
继承后的特点——构造方法
构造方法的作用时初始化成员变量的,所以子类的初始化过程中,必须限制性父类的初始化动作。子类的构造方法中默认有一个super(),表示调用父类的构造方法,父类成员变量初始化后,才可以给子类使用。
public class Fu { //父类
public Fu(int num){
System.out.println("父类构造方法");
}
}
public class Zi extends Fu{ //子类
public Zi(){
// super(); //系统自动赠送,但是在自己写上父类构造时自动消失
super(10);
System.out.println("子类构造方法");
}
}
super
super:代表父类的存储空间标识(可以理解为父亲的引用)
this:代表当前对象的引用
public class Fu { //父类
int num = 10;
public void method() {
System.out.println("父类方法");
}
}
public class Zi extends Fu { //子类
int num = 20;
public Zi() {
super();
}
public void methodZi() {
System.out.println(super.num);
}
public void method() {
super.method(); //super关键字的运用
}
}
继承的特点
- java只支持单继承,不支持多继承
- java支持多层继承
- 一个类只能有一个直接父类,但是可以有多个子类