java知识点复习新政版

该文章已生成可运行项目,

目录

源文件命名:

字节码文件  .class 

数据类型:

数据类型转换

自动转换   

 强制转换

自增自减运算符

关系运算符:

逻辑运算符

开关语句:

认识类 

构造方法 

一个实例

另一个实例

创建对象

调用类中方法   

类成员、类方法 static

继承,接口

super调用的规则与细节

方法重载,重写 

封装 

String 类

输入输出流 

垃圾回收 gc  (from 肘子秘籍)

命名规范 

数组 

包装类

集合类 List 


源文件命名:

Java源文件是编写Java程序的文本文件,通常以 .java 为后缀名。 一个Java源文件( .java )中最多只能有一个 public 修饰的类,且该类的名称必须与源文件名完全一致(包括大小写)。
object9.library 是一个包(package)                                        其中包含了 operation(操作)  ,  user(用户)两个包       operation 中放了可执行操作(类 c,接口 I)                        user 中为 用户的身份 管理员或普通用户                                                           

A.java

public class A { }
public class B { }  // 编译错误:源文件中只能有一个public类 

字节码文件  .class 

由Java源文件( .java )通过 javac 编译生成
javac - 将Java源文件( .java )编译为字节码文件( .class ),供Java虚拟机(JVM)执行。

数据类型:

类型分类类型名称字节数泛型对应包装类取值范围
整数型byte1 字节Byte-128 至 127
整数型short2 字节Short-32768 至 32767
整数型int4 字节Integer-2¹⁵ 至 2¹⁵-1(-32768 至 32767)
整数型long8 字节Long-2³¹ 至 2³¹-1(-2147483648 至 2147483647)
浮点型float4 字节Float±1.4E-45 至 ±3.4028235E+38(科学计数法)
浮点型double8 字节Double±4.9E-324 至 ±1.7976931348623157E+308
字符型char2 字节Character\u0000(0)至 \uFFFF(65535)
布尔型boolean1 字节 *Booleantrue 或 false

数据类型转换

自动转换   

取值范围小的转的大  精度低的转精度大的

一、数据类型按取值范围从小到大排序
 
1.  byte (-128~127,1字节)

2.  short (-32768~32767,2字节)

3.  char (0~65535,2字节,无符号)

4.  int (-2¹⁵~2¹⁵-1,4字节)

5.  long (-2³¹~2³¹-1,8字节)

6.  float (±1.4E-45~±3.4028235E+38,4字节,单精度)

7.  double (±4.9E-324~±1.7976931348623157E+308,8字节,双精度)

源类型(小范围 / 低精度)目标类型(大范围 / 高精度)
byteshort/int/long/float/double
shortint/long/float/double
charint/long/float/double
intlong/float/double
longfloat/double
floatdouble

 强制转换

小范围、低精度 转  大范围 大精度

eg:    Float floatVar = 3.14f;  
         int intVar = (int)floatVar;  // 强制转换Float→int

直接截断小数部分,不进行四舍五入:
​ (int)3.14f  →  3 

源类型(大范围 / 高精度)目标类型(小范围 / 低精度)
short/int/long/float/doublebyte
int/long/float/doubleshort
long/float/doubleint
float/doublelong
doublefloat
int/long/float/doublechar

自增自减运算符

运算符形式执行逻辑返回值示例(假设 i=5)
自增前缀++i先将 i 加 1,再使用 i 的新值加 1 后的值j = ++i → i=6,j=6
自增后缀i++先使用 i 的当前值,再将 i 加 1加 1 前的原值j = i++ → i=6,j=5
自减前缀--i先将 i 减 1,再使用 i 的新值减 1 后的值j = --i → i=4,j=4
自减后缀i--先使用 i 的当前值,再将 i 减 1减 1 前的原值j = i-- → i=4,j=5

关系运算符:

> , < , >=, <= , == , !=

逻辑运算符

&&  逻辑与  ,    || 逻辑或     ,   !逻辑取反

以上两种运算符 与C语言中不同的是      返回值 为  true 或 false   不是 1 , -1 , 0

if(a > b){}   a > b 为真的情况 返回 true 进入 if 的 代码块 { }   反之跳过
while(!a > b)   a > b 为真的情况 返回 true 但逻辑取反 ! 变为 false  不进入循环
                       a > b 为假的情况 返回 false 但逻辑取反 ! 变为 true  进入循环

开关语句:

执行过程:当switch后表达式的值和case语句后的值相同时,从该位置开始向下执行,直到遇到break语句或者switch语句块结束;如果没有匹配的case语句则执行default块的代码。

case 后加 :  不是 ;

package step4;

import java.util.Scanner;

public class HelloSwitch {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入月份:");
		
		int input = sc.nextInt();	//获取输入的月份
		
        //通过输入的月份来判断当前季节并输出
		/*****start*****/
		switch(input){
            case 3:
            case 4:
            case 5:
            System.out.println(input + "月是春天");
            break;
            case 6:
            case 7:
            case 8:
            System.out.println(input + "月是夏天");
            break;
            case 9:
            case 10:
            case 11:
            System.out.println(input + "月是秋天");
            break;
            case 12:
            case 1:
            case 2:
            System.out.println(input + "月是冬天");
            break;

            default:
                System.out.println("输入的月份无效,请输入 1 - 12 之间的整数");

        }
	
		

认识类 

class  类名{
      // 成员变量
      成员变量 a;   private/public/protected   变量名;
      成员变量 b;

      // 构造方法   初始化的作用
      public 类名(参数:可以没有 或 成员变量a,成员变量b){
      this.a = a;
      this.b = b;
}

      // 方法体  Getter   Setter
      public 返回值类型   getA (){
                return a;
}
      public void  setA(成员变量 a){
                this.a = a;
}
      public 返回值类型   getB (){
                return a;
}
      public void  setB(成员变量 b){
                this.a = b;
}

    // 主方法
      public static void main (String[] args){
}


  

构造方法 

一个实例
public class MyDate {
    public int year;
    //public int year = 2024;  就地初始化
    public int month;
    //public int month = 11;    就地初始化
    public int day;

   //代码块
//    {
//        year = 2024;
//        month = 11;
//        day = 21;
//          System.out.println("代码块初始化");
//    }

    //构造方法   一旦带有 void 就变为普通方法了
    public MyDate() {
        year = 2024;
        month = 11;
        day = 21;
        System.out.println("调用构造方法");
        //允许使用构造方法调用其他构造方法
       // this(2024,11,21);  this 只能做第一行代码

    }


    //    public void setDate(int y , int m ,int d){
//        year = y;
//        month = m;
//        day = d;
//    }
    public void setDate(int year , int month ,int day){
        // 优先将 year 作为形参 通过 this 关键字表明哪些是成员变量 特殊的引用 只能在方法中使用
        this.year = year;
        this.month = month;
        this.day = day;

    }
    public MyDate(int year, int month , int day){
        this.year =  year;
        this.month = month;
        this.day = day;
        System.out.println("调用第二个构造方法");

    }
    //this = new Date();  错误 this不是变量不能修改

    public void printDate(){
        System.out.println(year + "." + month + "." + day);
    }

    @Override    //在一个方法或类上 添加@  注解
    public String toString() {
        return "Date{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    public static void main(String[] args) {
 //       Date date = new Date();// 只是对 date 初始化了   int year int month int day 还未初始化 通过构造方法初始化  1.构造方法的方法名,必须和类名一致并且不能有返回值  2 new 对象时自动调用
//        date.setDate(2024, 11 ,21);  // 这样的方法调用,在编译器底层看到的效果: public void setDate(Date this, int year,int month, int day)
        MyDate date = new MyDate(2024,11,21);
        date.printDate();
    }
}
        //允许使用构造方法调用其他构造方法
另一个实例
public class Book {
    //书名
    private String name;
    //作者
    private String author;
    // 类型
    private String type;
    // 价格
    private double price;
    // 是否借出
    private boolean isBorrowed;

    // 构造方法
    public Book(String name, String author, String type, double price) {
        this.name = name;
        this.author = author;
        this.type = type;
        this.price = price;
        this.isBorrowed = false; // 默认未借出
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    @Override
    public String toString() {
//        return "Book{" +
//                "name='" + name + '\'' +
//                ", author='" + author + '\'' +
//                ", type='" + type + '\'' +
//                ", price=" + price +
//                ", isBorrowed=" + isBorrowed +
//                '}';
         return  "[" + name + "," + author + "," + price + "," + type + "," + (isBorrowed ? "已借出" : "在库中") + "]";
    }
}
创建对象

类名  对象名  =  new  类名;

eg:    Scanner   a = new  Scanner(System.in);    new 一个实例出来
就像在饭店吃饭   类是菜单  new 是点单的行为  对象即菜

 public Book(String name, String author, String type, double price) {
        this.name = name;
        this.author = author;
        this.type = type;
        this.price = price;
        this.isBorrowed = false; // 默认未借出
    }
上面那个 Book  book  =  new Book("《java从入门到精通》" ,“肘子” , “教材”,“100.00”);

                                                               name 书名      author 作者  type 书的类型 price 价格

括号内:  类的构造方法括号里的参数   若没有(即无参构造函数)可以不写

调用类中方法   

 对象名 .  方法;  “. ”点运算符
以Book 类 为例

Book  book  =  new Book("java从入门到精通" ,“肘子” , “教材”,“100.00”);

String bookName = book.getName();

System.out.printfln(bookName);  //  结果 《java从入门到精通》

类成员、类方法 static

关键词  static         eg:  static  int x;    静态成员/ 类成员
                                     static void  text()  {}       静态方法/ 类方法
注意:  类成员 可以用 类名访问不创建对象 
             类方法- 1.不能访问非static成员 
                          2. 内部不能使用 this 
                          3. 全局仅执行一次

3. 全局仅执行一次 

static  不能修饰 构造方法   选项中 this super 也可记下

 1.不能访问非static成员

继承,接口

继承 extends    接口  implements        Object  是所有类的父类(祖宗)

继承:  一个类可以有多个子类,但只能有一个父类       
final  关键字 修饰的类不能被继承 且 final 修饰的方法不能被重写   final 修饰的变量不能被修改
区别接口 : 一个类可以实现多个接口

public interface MyInterface { /* 接口内容 */ }

可定义 抽象方法(JDK 7 及以前默认 public abstract ,可省略修饰符 )、默认方法default 修饰,带方法体 )、静态方法static 修饰,带方法体 )、常量(默认 public static final ,必须赋值 )。

接口继承接口 :用 extends 关键字,支持多继承

interface SubInter extends InterA, InterB { } 

接口成员规则对比表

成员类型语法示例核心特性实现类 / 子类处理方式
抽象方法void doTask(); 隐式 public abstract 修饰(可省略)
无方法体,仅声明行为规范
非抽象类必须重写;抽象类可选择不重写,留待子类实现。
默认方法(JDK 8+)default void showMsg() { System.out.println("默认提示"); }用 default 修饰,必须包含方法体
为接口提供默认实现逻辑,不强制实现类修改
实现类可直接继承使用,也可重写覆盖。
静态方法(JDK 8+)static void log() { System.out.println("日志记录"); }用 static 修饰,必须包含方法体
只能通过接口名调用(如 MyInterface.log()
实现类无法继承或重写,需通过接口名直接调用。
常量String VERSION = "1.0";(等价于 public static final String VERSION = "1.0";隐式 public static final 修饰(可省略)
值必须固定,不可修改
接口和实现类均可通过 接口名.常量名 访问。

接口中声明的属性,会自动隐含 public static final  修饰符 。 

interface MyInterface {
    // 等价于 public static final int NUM = 10;
    int NUM = 10; 
}
 

接口中的私有方法:有普通私有(private )归默认方法调用,静态私有 (private static)归静态方法调用。接口中默认方法,静态方法都必须有方法体。所以私有也得有

//  父类  且为抽象类  abstract

public abstract class User {
    protected String name;
    public User (String name){
          this.name = name;
{

    // 显示用户菜单,返回值为用户输入的序号
    public abstract int menu();    // 抽象方法 必须在子类中重写(除非子类同为抽象类)

}

 //子类  Admin (管理员类) extends (继承自)User (用户类)

public class Admin extends User{

    public Admin(String name) {
        super(name);  //  通过 super 调用父类有参构造方法
        operations = new IOperation[] {
                new ExitOperation(),// 0
                new ListOperation(),// 1
                new FindOperation(),// 2
                new AddOperation(), // 3
                new DelOperation()  // 4
        };
    }

    @Override  // 重写抽象方法
    public int menu() {  // 与 int menu(); 保持一致
        // 打印管理员的菜单
        System.out.println("*********************************");
        System.out.println("欢迎您," + name);
        System.out.println("1.查看书籍列表");
        System.out.println("2.按照名字查找书籍");
        System.out.println("3.添加新书");
        System.out.println("4.删除书籍");
        System.out.println("0.退出");
        System.out.println("*********************************");
        System.out.println("请输入您的选项");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

super调用的规则与细节

1. 隐式 vs 显式调用

  • 隐式调用:若子类构造方法中省略super(...),编译器会自动插入super()(即调用父类的无参构造)。
    public Admin(String name) {
        // 隐式调用super(),等价于:
        // super();  // 必须在第一句
    
    }
    
  • 显式调用:当父类没有无参构造方法时,必须显式调用父类的其他构造方法。

 

方法重载,重写 


重载@overload :  指同一类中多个方法同名,但参数列表不同(参数数量、类型或顺序不同),编译器会根据调用时的参数自动匹配对应方法。

. 必须满足的条件

1- 方法名相同:如 calculate() 。

2- 参数列表不同(至少满足以下一项):

3- 参数数量不同:如 calculate(int a) 和 calculate(int a, int b) 。

4- 参数类型不同:如 calculate(int a) 和 calculate(double a) 。

5- 参数顺序不同:如 calculate(int a, double b) 和 calculate(double a, int b) 。

6- 返回值类型、访问修饰符无关:即使返回值不同,只要参数列表不同就构成重载(但仅返回值不同不能重载)。

重写 @override:是子类对父类同名方法的重新实现,体现了“运行时多态”特性。
必须满足的条件

1- 子类继承父类或实现接口。

2- 方法名、参数列表、返回值类型完全相同(例外:子类返回值类型是父类返回值的子类)。

3- 子类方法的访问修饰符不能比父类更严格(如父类方法是 protected ,子类不能是 default )。 

例题

封装 

 把对象的属性(成员变量)和实现细节藏起来,通过公共方法(getter/setter) 对外提供访问,让外部无法直接修改内部状态

Java 用 访问修饰符 控制类成员的可见范围,常用的有:

修饰符可见范围典型场景
private仅当前类可见隐藏核心属性 上面Book 类的 name .... 都用了private  提供了  getter  setter  写法见上
default同包可见(不写修饰符时默认)包内工具类的内部协作 
protected同包 + 子类可见父类给子类开放的扩展入口
public所有类可见对外暴露的核心功能(如main方法)

 

String 类

 1. 不可变性:对象创建后内容不可修改,修改操作会生成新对象



2. 创建方式:

1.类似 String str = "abc",字符串入常量池,相同内容复用。

2.String str = new String("abc");  


3. 字符串拼接:

 +  拼接,  int a = 123   int b = 456   a + ""

4. 比较方式

-  equals()  比较内容, ==  比较对象引用。
String s1 = new String("a");
String s2 = new String("a");
System.out.println(s1 == s2);   // false   s1 s2 引用(地址不同)
System.out.println(s1.equals(s2));   //  true  s1 s2 内容都是 “a”

-  compareTo()  按字典序比较,返回差值。从左到右一个字符一个字符比 ASCII 码

5. 常用方法: length()  获长度、 charAt(index)  取指定字符、 substring()  截取子串、 toLowerCase() / toUpperCase()  转大小写、 split()  按规则拆分、 replace() / replaceAll()  替换等。

6. 与 StringBuilder、StringBuffer 区别: String  不可变,后两者可变,前者线程不安全、效率高,后者线程安全、效率稍低,大量拼接选后两者。

拼接  1 创建   StringBuffer  stringBuffer = new  StringBuffer();
         2 调方法  s.append("要拼接的字符串");



7. 注意空值与空字符串: null  表示无对象, ""  是有对象但内容空,调用方法前需判空,避免空指针异常。

异常

受查异常(编译时异常,必须处理)

异常类名说明
IOException输入输出操作异常(如文件不存在、读写失败)
SQLException数据库操作异常(如 SQL 语法错误、连接失败)
ClassNotFoundException类未找到异常(如通过反射加载类时类不存在)
InterruptedException线程中断异常(如线程等待时被中断)
FileNotFoundException文件未找到异常(如访问不存在的文件路径)

非受查异常(运行时异常,无需显式处理)

异常类名说明
NullPointerException空指针异常(访问 null 对象的方法或属性)
ArrayIndexOutOfBoundsException数组越界异常(访问不存在的数组索引)
ClassCastException类型转换异常(如将 Object 强制转换为不兼容类型)
IllegalArgumentException非法参数异常(如方法传入无效参数)
ArithmeticException算术异常(如除以 0)
IllegalStateException状态异常(如集合已修改时迭代)
IndexOutOfBoundsException索引越界(如 String、List 访问不存在的索引)
NumberFormatException数字格式异常(如Integer.parseInt("abc")

异常处理关键字与语法

关键字作用
try包裹可能抛出异常的代码块
catch捕获并处理特定类型的异常
finally无论是否发生异常,都会执行的代码块(常用于资源关闭)
throw在方法中手动抛出异常对象
throws方法声明时抛出异常,由调用方处理

代码结构 

//        try{
//           可能发生异常的代码
//        }catch(){
//           处理异常(打印异常信息)
//        }finally{  //  无论上方的 try 是否抛出异常
//                       此处的 finally 都是会执行的 (收尾工作)
//        }

输入输出流 

需要导入包   import java.io*;

一、字节流与字符流体系
 
1. 字节流(处理二进制数据 )

-  FileInputStream :从文件读字节数据,构造方法传文件路径, read  方法读字节(返回 -1 表示读完 )。

-  FileOutputStream :向文件写字节数据,构造方法可传  (路径, true)  实现追加(默认覆盖 )。

-  DataOutputStream :搭配其他输出流,写基本数据类型(writeInt 、 writeDouble  )。

2. 字符流(处理文本数据 )

-  FileReader :从文件读字符,适合文本文件, read  方法读字符或字符数组。

-  FileWriter :向文件写字符,默认覆盖
 
二、缓冲流(提升读写效率 )
 
-  BufferedInputStream / BufferedReader :构造方法需传入基础字节流/字符流(如  FileInputStream / FileReader  ),通过缓冲区减少 IO 次数。
 
三、文件与目录操作
 
-  isDirectory() : File  类方法,判断  File  对象是否表示目录。

-  RandomAccessFile :支持随机访问文件,可灵活读写文件任意位置(如  seek  跳转、 read/write  随机读写 )。
 
四、异常与资源管理
 
- 流操作会抛  IOException (如文件不存在抛  FileNotFoundException  ),需用  try-catch  捕获或  throws  声明。


 

import java.io.*; // 导入 IO 相关类

public class Example10_4 {
    public static void main(String args[]) {
        int n = -1; // 用于存储每次读取的字节数
        byte[] a = new byte[100]; // 缓冲区,每次最多读 100 字节

        try {
            // 1. 创建 File 对象,关联要读取的文件
            File f = new File("Example10_4.java"); 
            // 2. 创建文件字节输入流(字节流,读二进制数据)
            FileInputStream in = new FileInputStream(f); 

            // 3. 循环读取:read(a, 0, 100) 读入字节数组,返回实际读取的字节数
            while ((n = in.read(a, 0, 100)) != -1) { 
                // 4. 将字节数组转为字符串(从 0 开始,取 n 个字节)
                String s = new String(a, 0, n); 
                // 5. 输出字符串内容(拼接显示文件内容)
                System.out.print(s); 
            }

            in.close(); // 6. 关闭流
        } catch (IOException e) { 
            // 7. 捕获 IO 异常(文件不存在、读写失败等)
            System.out.println("File read Error" + e); 
        }
    }
}

import java.io.*; // 导入 IO 相关类

public class Example10_5 {
    public static void main(String args[]) {
        // 1. 将字符串转为字节数组
        byte[] a = "国庆 70 周年".getBytes(); 
        byte[] b = "十一快乐".getBytes(); 

        try {
            // 2. 创建文件字节输出流(关联文件 happy.txt,默认覆盖内容)
            FileOutputStream out = new FileOutputStream("happy.txt"); 
            out.write(a); // 3. 写入字节数组 a(完整内容)
            // 4. 写入字节数组 b(从索引 0 开始,写 b.length 个字节)
            out.write(b, 0, b.length); 
            out.close(); // 5. 关闭流
        } catch (IOException e) { 
            // 6. 捕获 IO 异常
            System.out.println("Error " + e); 
        }
    }
}

垃圾回收 gc  (from 肘子秘籍)

Java是自动内存管理语言,垃圾回收由JVM的GC机制自动执行,无需程序员手
动释放内存。
核心原则:GC负责识别并回收不再被引用的对象所占用的内存。

程序员无法直接干预GC的执行时机,只能通过`System.gc()建议GC运行,但
JVM不一定立即执行。
不存在“指定时间释放内存对象”的编程接口,GC的触发时机由JVM内部策略决
定(如内存阈值、对象存活时间等)。

 总结:Java 的 GC 是“自动、智能、难干预”的内存回收机制:自动识别无用对象,回收时机由 JVM 自己定,程序员只能建议( System.gc() ),但不一定被采纳 。

命名规范 

分类具体规则示例(正确 / 错误对比)
允许字符字母(A - Z、a - z)、美元符号($)、下划线(_)、数字(0 - 9,不能开头)正确:studentName_idprice\$10
错误:1num(数字开头)
不建议内容中文但可以 class  人{}
大小写规则严格区分大小写,Name 和 name 是不同标识符正确:UserDao userService
关键字避免禁止使用 Java 关键字(int/class/while 等)作为标识符错误:int(变量名用 count 替代)
类名规范大驼峰(每个单词首字母大写,无连接符)如: User   Admin
方法、变量名规范小驼峰(首单词小写,后续单词首字母大写),描述行为 / 属性如:calculateTotal() productPrice
常量名规范全大写,单词间用下划线分隔,强调不可变性如:MAX_RETRY DEFAULT_TIMEOUT

数组 

java 数组 

 创建 :  int [ ]  arr  = {1,2,3};

               int  arr [ ] = {1,2,3};

               int [ ]  arr  = new  int [3];

               int arr[ ]   =  new int [3];

数组下标:

eg:

int[] arr = {10, 20, 30};  
arr[0] = 10;  // 第一个元素  
arr[2] = 30;  // 第三个元素(最大索引 2 = 长度 3 - 1)

int len = arr.length;   //  获取数组长度  3

数组遍历

for (int i = 0; i < nums.length; i++) {  
    System.out.println(nums[i]);  
}  

// for-each 遍历
for (int num : nums) {  
    System.out.println(num);  
}

二维数组声明:
1.   int[][] arr = {{3,3}};
2.   int[][] arr = new int[3][4];3行4列
3.   int[][] arr = new int[3][]      3行,暂不配列数
后会为每行分配列数
arr[0] = new int[2]       第1行2列
arr[1] = new int[3]       第2行3列

核心思想双指针: 

结果:   [4,5,3,1,2] 

代码实现 

//  核心思想

public void reverseArray (int[] arr){
   
int left = 0;
int right = arr.length - 1;

while(left < right){
    int tmp = arr[left];
    arr[left] = arr[right];
    arr[right] = tmp;
    left ++;
    right --;
   
}


泛型,集合类 

import java.util.*;

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        List<Integer> list = new ArrayList<Integer>();
        while (sc.hasNext()) {
            list.add(sc.nextInt());
        }
        
 // list数组反转
        int left = 0;
        int right = list.size() - 1;
        while(left<right){
            Integer tmp = list.get(left);
            list.set(left,list.get(right));
            list.set(right,tmp);
            left++;
            right--;
        }
  // 循环输出list数组元素
        for(int i = 0; i < list.size(); i++){
            //System.out.println(Arry.toString(list);
            System.out.print(list.get(i) + " "); 
        }

    }
}

题目给了list, 创建了一个线性表并实现了多组输入通过list.add()方法将数组元素入表

此时 数据类型变成了Integer 所以创建交换变量(tmp)时 应使用这个作为数据类型 

list.get() 获取right的值 

list.set()    将 right的值  赋给 left    将 right 设为 tmp (即left的值)   交换完成

left++  指针向后走一步       right--    指针向前走一步

list.size()  获取元素个数

包装类

基本类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

集合类 List 

一个接口类型,表示有序、可重复的集合

实现类底层结构

基础方法

ArrayList动态数组.add()   .remove()  .set()  .get() 增删改查  .size() 元素个数
LinkedList双向链表.add()   .remove()  .set()  .get() 增删改查  .size() 元素个数

创建:List<Integer> list = new ArrayList<Integer>();

Integer:泛型参数,指定列表元素类型为包装类 Integer 写 <> 里面  后一个可省略

List<Integer> list = new ArrayList<>();

未完待续......

     我看见  半晴天  好刺眼   好刺眼   它讲别怕往前        ———— DT        《半晴天》 

本文章已经生成可运行项目
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值