前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java基础教程(全代码解析)

Java基础教程(全代码解析)

作者头像
达达前端
发布2022-04-29 08:51:10
7660
发布2022-04-29 08:51:10
举报
文章被收录于专栏:达达前端
这里写图片描述
这里写图片描述

字面量:

整数字面量为整型(int) 小数字面量为双精度浮点型(double)

数据类型: byte short int long float double

接下来代码展示理解

代码语言:javascript
复制
public class Test{
 char c = 'a';
 switch(c){
  case 'b':
  System.out.println('b');
  break;
  case 'c':
  System.out.println('c');
  break;
  case 'a':
  System.out.println('a');
  break;
  default:
  System.out.println('d');
 }
} 
}

swith( char byte short int)只允许四种类型

代码语言:javascript
复制
public class Test{
 public static void main(String args[]){
  int score = 90;
  if(score > 85 && score <= 100){
   System.out.println("成绩为优");
  }
  else if(score > 75 && score <= 85){
   System.out.println("成绩为良");
  }
  else if(score > 60 && score <= 75){
   System.out.println("成绩为中");
  }
  else if(score <= 60 && score >= 0){
   System.out.println("成绩为差");
  }
  else if(score > 100 || score < 0){
   System.out.println("成绩不在正常的范围之内");
  }
 }
}
代码语言:javascript
复制
for(int i = 0; i < 10; i++){
 System.out.println(i);
}
代码语言:javascript
复制
public class Test{
 public static void main(String args[]){
  int i = 0;
  while(i < 10){
   System.out.println(i);
   i++;
  }
 }
}

打印100-200的素数:

代码语言:javascript
复制
class Test{
 public static void main(String args[]){
  for(int i = 100; i < 201; i++){
   boolean b = false;
   
   for(int j = 2; j < i-1; j++){
    int k=i%j;
    if(k==0){
     b=true;
    }
   }
   //如果不是true就打印出素数
   
  if(!b){
   System.out.println(i);
  }
  }
 }
}
代码语言:javascript
复制
public class Test{
 public static void main(String args[]){
  int i = 5;
  int j = i++ + 5;
  System.out.println(i);
  System.out.println(j);
  }
}
代码语言:javascript
复制
j=10;
i=6;

int j = ++i + 5;
System.out.println(j);
j=11;
i=6;

&逻辑与 &&短路与

代码语言:javascript
复制
class Test{
 public static void main(String args[]){
  for(int i=1; i<5; i++){
   for(int j=0; j<4-i; j++){
    System.out.print(" ");
   }
   for(int k=0; k<i; k++){
    System.out.print("* "):
   }
   System.out.println("");
   }
  }
}

什么是面向对象?

面向对象是一种编程方式 面向对象是一种思维方式 面向对象不是一种编程语言

应该如何学习面向对象?

掌握一门面向对象语言的语法 掌握面向对象的思维方式 熟悉面向对象设计原则 掌握面向对象设计模式

什么是面向对象思维方法?

确定谁来做,其次确定怎么做 考虑整体,其次考虑局部 首先考虑抽象,其次考虑具体

不要认为掌握了一门面向对象语言就是掌握了面向对象

习惯了将面向对象与现实世界做比较

创建类的方法

创建对象的方法

对象和对象的引用 定义类的方法

代码语言:javascript
复制
class类名
{
属性;
方法;
}

属性也叫成员变量,主要用于描述类的状态 方法也叫成员方法,主要用于描述类的行为

生成对象的方法

格式:类名 对象名=new 类名() 例如:Dog dog = new Dog(); 创建一个Dog的引用 创建一个Dog的对象 类和对象的关系 类是抽象,而对象是具体的。

变量的类型 变量的值 Java的数据类型,基本数据类型和引用数据类型

对象就是引用数据类型 生成对象的方法 Java虚拟机把内存分为栈内存和堆内存

对象就叫引用数据类型 应该如何学习面向对象 面向对象的思维方式

代码语言:javascript
复制
class Test{
 public static void main(String args[]){
  Dog d = new Dog();
  d.name="哈哈";
  d.age=2;
  d.jump();
  System.out.println("名字是"+d.name);
  }
}

对象的使用方法,多对象的创建方法,匿名对象的创建和使用方法。 对象的使用方法 使用对象调用变量和函数 对象.变量 对象.函数() 生成多个对象 匿名对象的使用 可以不定义对象的引用名称,而直接调用这个对象的方法,这样的对象叫做匿名对象 如:new Dog().jump();

函数的重载和构造函数的作用

重载的表达

代码语言:javascript
复制
class A{
 void funA(){
  System.out.println("没有参数的funA函数");
 }
 void funA(int i){
  System.out.println("有参数的funA函数");
 }
 void funA(int i,double d){
  System.out.println("拥有两个参数的funA函数");
 }
}

class Test{
 public static void main(String args[]){
  A a = new A();
  a.funA();
  a.funA(1,2.0);
  }
}

什么叫函数的重载呢?

两个或者多个函数在同一个类当中; 函数名相同; 参数列表不同

什么是构造函数?

代码语言:javascript
复制
A(){
}

使用this调用成员变量和成员函数 使用this调用构造函数 静态成员变量只有一份 在静态函数当中不能使用this 静态代码块的主要作用是为静态成员变量赋值

代码语言:javascript
复制
class Person{
 static{
  System.out.println("dd"):
 }
 static String name;
 ..
}

继承,封装,多态

什么是继承?

在现实世界当中,继承就是儿子得到老子的东西,在面向对象的世界当中,继承就是一个类得到了另一个类当中的成员变量和成员方法;

Java当中只支持单继承 Java只支持单继承,不允许多继承 使用继承是为了减少重复代码

子类实例过程: 生成子类过程: 使用super调用父类构造函数的方法

代码语言:javascript
复制
class Person{
 String name;
 int age;
 Person(){
  System.out.prinltn("Person的无参数构造函数");
 }
 Person(String name,int age){
  this.name=name;
  this.age=age;
  System.out.println("Person有参数的构造函数");
 }
 
 void eat(){
  System.out.println("定义吃饭的方法");
 }
}

class Student extends Person{
 //子类继承父类
 Student(){
  //父类
  super();
  System.out.println("Student的无参数构造函数");
 }
 Student(String name,int age,int id){
  super(name,age);
  this.id=id;
 }
}

class Test{
 public static void main(String args[]){
  Student student = new Student();
 }
}

虽然子类不能继承父类的构造函数,但我能用super()来调用父类的构造函数。 调用子类的构造函数,一定会调用父类的构造的函数。

函数的复写(override),也称为覆盖或者重写

使用super调用父类的成员函数

什么是复写?

在具有父子关系的两个类当中,父类和子类各有一个函数,这两个函数的定义(返回值类型,函数名,参数列表)完全相同

对象的转型(多态性地体现)

什么是向上转型?向上转型就是将子类的对象赋值给父类的引用。 什么是向下转型?向下转型就是将父类的对象赋值给子类的引用。

代码语言:javascript
复制
Student s = new Student();
Person p = s;

//一个引用能够调用哪些成员(变量和函数),取决于这个引用类型 //一个引用调用的是哪一个方法,取决于这个引用所指向的对象

代码语言:javascript
复制
class Student extends Person{
 String address;

 void introduce(){
  super.introduce();
  System.out.println("我家在"+address);
 }
}

class Test{
 public static void main(String args[]){
  String s = new Student();
  Person p = s;
  
  p.name = "hhh";
  p.age = 20;
  p.introduce();
  }
}

向下转型:
Student s1 = new Student();
Person p = s1;
Student s2 = (Student)p;

抽象类?

类是抽象的,对象是具体的,比类更抽象的,就是抽象类 先抽象,后具体

什么是抽象函数?

只有函数的定义,没有函数体的函数被称为抽象函数 abstract void test();

什么是抽象类?

使用abstract定义的类被称之为抽象类 抽象类不能够生成对象; 如果一个类当中包含有抽象函数,那么这个类必须被声明为抽象类 如果一个类当中没有抽象函数,那么这个类也可以被声明为抽象类

抽象类不能实例化,继承抽象类,那么该类必须为抽象类 如何调用抽象类,用复写的方法

代码语言:javascript
复制
abstract class Person{
 abstract void eat();
}

class Chinese extends Person{
 void eat(){
  System.out.pritln("hhh");
 }
}
 
class Test{
 public static void main(String args[]){
  Person p = new Chinese();
  p.eat();
 }
}

代码:
abstract class Person{
 Person(){
  System.out.println("Person没有参数的构造函数");
 }
 
 Person(String name,String age){
  this.name=name;
  this.age=age;
 }
 
 String name;
 int age;
 void introduce(){
  System.out.println("我的名字是"+name+",我的年龄是"+age);
 }
 abstract void eat();
}
}

class Chinese extends Person{
 String address;
 Chinese(){
  super();
  System.out.println("Chinese的构造函数"); 
  }
  
  Chinese(String name,int age,String address){
   super(name,age);
   this.address=address;
  }
  void eat(){
   //复写
   System.out.println("吃饭");
  }
}

class Test{
 public static void main(String args[]){
  Person p = new Chinese();
  p.eat();
  }
}

虽然不能调用抽象类的构造函数,但是可以通过用子类来调用抽象函数,生成子类对象的时候使用调用构造函数。抽象函数是没有函数体的函数,用abstract来定义抽象类。

包和访问权限的语法

Java中的访问权限 public:公共权限 private:私有权限 default:默认权限 protected:受保护权限

public > protected > default > private

什么是接口(interface)

接口中的方法都是抽象方法,public权限,全是抽象函数,不能生成对象

代码语言:javascript
复制
interface Student{
 public void read();
 public void write();
}
代码语言:javascript
复制
class ChineseStudent implements Student{
 //复写
 public void read(){
  System.out.println("read");
 }
 public void write(){
  System.out.println("write");
 }
}

class Test{
 public static void main(String args[]){
  ChineseStudent chinesestudent = new ChineseStudent();
  Student student = chinesestudent;
  
  student.read();
  student.write();
 }
}

接口的基本语法如上

实现接口用implements关键字,一个接口可以实现多个接口,一个接口可以继承多个接口

代码语言:javascript
复制
interface Student{
 public void read();
 public void write();
}
 
interface Teacher{
 public void teach();
 public void test();
}

class Person implements Student,Teacher{
 public void read(){
  System.out.println("read"):
 }
 public void write(){
  System.out.println("write");
 }
 public void teach(){
  System.out.println("teach"):
 }
 public void test(){
  System.out.println("test"):
 }
}

class Test{
 public static void main(String args[]){
  Person person = new Person();
 
  Student student = person;
  student.read();
  student.write();
 
  Teacher teacher = person;
  teacher.teach();
  teacher.close();
 }
}

工厂方法模式:
interface Printer{
 public void open();
 public void close();
 public void print(String s);
}

class Printer1 implements Printer{
 public void open(){
  System.out.println("printer1 open");
 }
 public void close(){
  System.out.println("printer1 close");
 }
 public void print1(String s){
  System.out.println("print1"+s);
 }
}

class Printer2 implements Printer{
 private void clean(){
  System.out.println("printer2 clean");
 }
 public void close(){
  this.clean();
  System.out.println("print2 close");
 }
 
 public void open(){
  System.out.println("print2 open");
 }

 public void print(String s){
  System.out.println("print2"+s);
 }
}

class Test{
 public static void main(String args[]){
  //根据用户的选择
  printer.open();
  printer.print("test");
  printer.close();
  }
}

工厂
class PrinterFactory{
 public static Printer getPrinter(int flag){
  Printer printer = null;
 
  //int flag = 0;
  
  if(flag == 0){
   printer = new printer1();
  }
  else(flag == 1){
   printer = new CanonPrinter();
  }
  
  return printer;
 }
}

class Test{
 public static void main(String args[]){
  //Printer gerPrinter(int flag)
  int flag = 1;
  Printer printer = PrinterFactory.getPrinter(flag);
  printer.open();
  printer.print("test");
  printer.close();
 }
}

Java中的异常
什么是异常?
try...catch...finally结构的使用方法

class Test{
 public static void main(String args[]){
  
  try{ 
 
   int i = 1 / 0;
   }
   catch(Exception e){
    e.printStackTrace();
   }
   finally{
    System.out.println("finally");
   }
   System.out.println(5);
  }
}

class Test{
 public static void main(String args[]){
  try{
   Thread.sleep(1000);
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
}

throw和throws的作用区别:
用代码表示:

class Person{
 private int age;

 public void setAge(int age) throws Exception{
  if(age<0){
   RuntimeException e = new RuntimeException("年龄不能小于0");
   throw e;
  }
  this.age = age;
  }
}

class Test{
 public static void main(String args[]){
  Person person = new Person();
  try{
   person.setAge(-1);
  }
  catch(Exception e){
   System.out.println(e);
  }
 }
}

throws Exception谁调用谁处理

Java当中的IO

IO分为三种:

第一种: 输入流 输出流 第二种: 字节流 字符流 第三种分类: 节点流 处理流

IO当中的核心类 InputStream OutputStream FileInputStream FileOutputStream 字节流的核心类 InputStream OutputStream

记住这个 InputStream: int read(byte[] b,int off,int len) OutputStream: void write(byte[] b,int off,int len)

FileInputStream

代码语言:javascript
复制
class Test{
 public static void main(String args[]){
  FileInputStream fis = null;
  try{
   fis = new FileInputStream("e:/read.txt");
   byte[] buffer = new byte[100];
   fis.read(buffer,0,buffer.length);
  
   for(int i = 0;i<buffer.length;i++){
    System.out.println(buffer[i]);
   }
  }
  catch(Exception e){
   System.out.println(e);
  }
 }
}

字符表达
FileInputStream和FileOutputStream
 
class Test{
 public static void main(String args[]){
  FileInputStream fis = null;
  FileOutputStream fos = null;
  
  try{
   fis = new FileInputStream("e:/read.txt"):
   fos = new FileOutputStream("e:/write.txt");
  
   byte[] buffer = new byte[100];
   int temp = fis.read(buffer,0,buffer.length);
   fos.write(buffer,0,temp);
  
   //String s = new String(buffer);
   //s = s.trim();
   //System.out.println(s);
  }
  catch(Exception e){
   System.out.println(e);
  }
 }
}
  
优化:
class Test{
 public static void main(String args[]){
  //声明输入流,输出流引用
  FileInputStream fis = null;
  FileOutputStream fos = null;
  
  try{
   fis = new FileInputStream("e:/read.txt");
   fos = new FileOutputStream("e:/write.txt");
  
   byte[] buffer = new byte[1024];
   while(true){
    int temp = fis.read(buffer,0,buffer.length);
    if(temp == -1){
     break;
    }
    fos.write(buffer,0,temp);
  }
  }catch(Exception e){
   System.out.println(e);
  }
  finally{
   try{
    fis.close(); 
    fos.close();
  }
  catch(Exception e){
   System.out.println(e);
  }
 }
}
}

字符流

读写文件时,以字符为基础为字符流

字节输入流:Reader <-- FileReader
int read(char[] c,int off,int len)
字节输出流:Writer <-- FileWriter
void write(char[] c,int off,int len)

public class TestChar{
 public static void main(String args[]){
  FileReader fr = null;
  FileWriter fw = null;
  try{
   fr = new FileReader("e:/read.txt");
   fw = new FileWriter("e:/write.txt");
   
   char[] buffer=new char[100];
   int temp = fr.read(buffer,0,buffer.length);
   fw.write(buffer,0,temp);
   }
   catch(Exception e){
    System.out.println(e);
   }
   finally{
    try{
    }
    catch(Excepiton e){
     System.out.println(e); 
    }
   }
}

节点流和处理流 处理流使用实例

装饰者模式

节点流与处理流的关系

BufferedReader介绍

一行一行的读取

BufferedReader使用方法,生成BufferedReader对象的方法

BufferedReader int = new BufferedReader(new FileReader(“from.int”));

处理流,Reader,Writer以及他们所有的子类都属于字符流 BufferedReader属于字符流,处理流,然后呢?它又是处理流

BufferedReader全称字符输入处理流

FileReader 和 BufferedReader

代码语言:javascript
复制
class Test{
 public static void main(String args[]){
  FileReader fileReader = null;
  BufferReader bufferReader = null;
  try{ 
   fileReader = new FileReader("e:/read.txt");
   bufferedReader = new BufferedReader(fileReader);
   String line = null;
   while(true){
    line = bufferedReader.readLine();
    if(line == null){
     break;
    }
    System.out.println(line); 
   }
  
   //String line = bufferedReader.readLine();
   //System.out.println(line);
   }
   catch(Exception e){
    System.out.println(e);
    }
    finally{
     try{
      bufferedReader.close(); 
      fileReader.close();
     }
     catch(Exception e){
      System.out.println(e);
     }
    }
   }
}

装饰者模式
interface worker{
 public void doSomeWork();
}

class worker1 implements worker{
 public void doSomeWork(){
  System.out.println("work1");
 }
}

class worker2 implements worker{
 public void doSomeWork(){
  System.out.println("work2");
 }
}

class A implements worker{
 private Worker worker;
 
 public A(Worker worker){
  this.worker=worker;
 }
 public void doSomeWork(){
  System.out.println("哈哈");
  worker.doSomeWork();
 }
}

class Test{
 public static void main(String args[]){
  Worker1 worker1 = new Worker1();
  A a = new A(worker1);
  a.doSomeWork();

  Worker2 worker2 = new Worker2();
  A a2 = new A(worker2);
  a2.doSomeWork();
 }
}

什么是内部类?

class A{
 class B{
 }
}

如何生成内部类的对象?

class Test{
 public static void main(String args[]){
  A a = new A();
  
  A.B b = new A().new B();
  //或者A.B b = a.new B();
 }
}

class A{
 int i;
 class B{
  int j;
 
  int funB(){
   int result = i+j;
   return result;
  }
 }
}

class Test{
 public static void main(String args[]){
  A a = new A();
  A.B b = a.new B();
 
  a.i=3;
  a.j=1;
  
  int result = b.funB();
  System.out.println(result);
  }
}

class A{
 int i;
 class B{
  int j;
  int funB(){
   int result = A.this.i+this.j;
   return result;
  }
 }
}

匿名内部类
interfacce A{
 public void doSomething();
}

class B{
 public void fun(A a){
  System.out.println("B函数");
  a.doSomething();
 }
}

class Work implements A{
 public void doSomething(){
  System.out.println("doSomething");
 }
}

class Test{
 public static void main(String args[]){
  Work work = new Work();
  A a = work;
 
  B b = new B();
  b.fun(a);
  }
}

匿名内部类
class Test{
 public static void main(String args[]){
  B b = new B();
  b.fun(new A(){
   public void doSomething(){
    System.out.println("匿名内部类");
   }
  });
 }
}

Java中的线程

多进程:就是指在操作系统中能同时运行多个任务程序
多线程:在同一个应用程序中多个顺序流同时执行

创建线程的方法
定义一个线程类,它继承类Thread并重写其中的方法run();
方法run()称为线程体;
由于Java只支持单继承,用这种方法定义的类不能再继承其他类。

class FirstThread extends Thread{
 public void run(){
  for(int i=0;i<100;i++){
   System.out.println("FirstThread"+i);
  }
 }
}

class Test{
 public static void main(Sting args[]){
  FirstThread ft = new FirstThread();
  ft.start();
  
  for(int i = 0; i<100;i++){
   System.out.println("main"+i):
  }
 }
}

提供接口Runnable的类作为线程的目标对象,在初始化一个Thread类或者Thread子类的线程对象时,把目标对象传递给这个线程实例,由该目标对象提供线程体。

class Test implements Runnable{
 public void run(){
  for(int i = 0;i<100;i++){
   System.out.println("Runnable"+i);
  }
 }
}

class Test{
 public static void main(String args[]){
  Test test = new Test();
  Thread t = new Thread(test);
  System.out.println(t.getPriority());
  t.start();
 }
}

线程的简单控制方法

中断线程 Thread.sleep(); Thread.yield();//让出自己正在使用的CPU

设置线程的优先级 getPriority(); setPriority();

代码语言:javascript
复制
class Test implements Runnable{
 public void run(){
  for(int i = 0;i<100;i++){
   System.out.println("Runnable"+i);
   if(i==50){
    try{
     Thread.sleep(2000);
    }
    catch(Exception e){
     System.out.println(e);
    }
   }
  }
 }
}

class Test{
 public static void main(String args[]){
  RunnableImp1 ri = new RunnableImp1();
  Thread t = new Thread(ri);
  
  t.setPriority(Thread.MAX_PRIORITY);
  //t.setPriority(Thread.MIN_PRIORITY);
  
  t.start();
  System.out.println(t.getPriority());
  }
}

class Test{
 public static void main(String args[]){
  MyThread myThread = new MyThread();

  Thread t1 = new Thread(myThread);
  Thread t2 = new Thread(myThread);
 
  t1.setName("线程1");
  t2.setName("线程2");

  //分别启动
  t1.start();
  t2.start();
 }
}

class MyThread implements Runnable{
 int i = 100;
 public void run(){
  while(true){
   System.out.println(Thread.currentThread().getName()+i);
   i--;
   Thread.yield();
   if(i<0){
    break;
   }
  }
 }
}

//同步代码块
class MyThread implements Runnable{
 int i = 100;
 public void run(){
  while(true){
   synchronized(this){
    System.out.println(Thread.currentThread().getName()+i);
    i--;
    Thread.yield();
    if(i<0){
     break;
    }
   }
  }
 }
}


深入synchronized关键字

class Service{
 public void fun1(){
  synchronized(this){
   try{
    Thread.sleep(3*1000);
   }
   catch(Exception e){
    System.out.println("fun1");
   }
  }
  
 public void fun2(){
  synchronized(this){
   System.out.println("fun2");
  }
 }
}

class MyThread1 implements Runnable{
 private Service service;
 public MyThread1(Service service){
  this.service = service;
 }
 public void run(){
  service.fun1();
 }
}

class MyThread2 implements Runable{
 private Service service;
 public MyThread2(Service service){
  this.service = service;
 }
 public void run(){
  service.fun2();
 }
}

class Test{
 public static void main(String args[]){
  Service service = new Service();
  Thread t1=new Thread(new MyThread1(service));
  Thread t2=new Thread(new MyThread2(service));
 
  t1.start();
  t2.start();
 }
}

同步锁 锁住的是service
同步方法,同步代码块锁住this

class Service{
 public synchronized void fun1(){
  try{
   Thread.sleep(3*1000);
  }
  catch(Exception e){
   System.out.println(e);
  }
  System.out.println("fun1");
 }
 public void fun2(){
  synchronized(this){
   System.out.println("fun2");
 }
 }
}

JAVA当中的数组


class Test{
 public static void main(String args[]){
  //数组的静态声明
  int arr [] = {5,2,7,8,9,0};

  arr[3] = 10;
 
  //System.out.println(arr[3]);

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

  }
}

class Test{
 public static void main(String args[]){
  int arr[] = {2,4,6,7,8};
  
  System.out.println(arr.length);
  }
}

数组的动态声明
class Test{
 public static void main(String args[]){
 
 //动态声明
 int arr [] = new int [10];
 System.out.println("arr数组长度"+arr.length);
 
   for(int i = 0;i<arr.length;i++){
    System.out.println(arr[i]);
   }
 }
}

二维数组定义方法
class Test{
 public static void main(String args[]){
  //二维数组的定义方法,长度为3

   int arr [][] = {{1,2,3},{4,5,6},{7,8,9}};
   
   System.out.println(arr[1][1]);

   for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++){
     System.out.println(arr[i][j]);
    }
   }

 }
}

优化
class Test{
 public static void main(String args[]){
  //二维数组的定义方法,长度为3

   int arr [][] = {{1,2,3},{4,5,6},{7,8}};
   
   System.out.println(arr[1][1]);

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

 }
}

动态
class Test{
 public static void main(String args[]){

   //int arr [][] = {{1,2,3},{4,5,6},{7,8}};
   
   int arr [][] = new int[3][5]; 
  
   System.out.println(arr[1][1]);

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

 }
}

类集框架

类集框架是一组类和接口,位于java.util包,主要用户存储和管理对象,主要分为三大类—集合,列表和映射。

什么是集合(Set)

集合中的对象不按特定的方式排序,并且没有重复对象;

对象是没有顺序的,集合是没有顺序的

什么是列表(List)

集合中对象按照索引位置排序,可以有重复的对象。 可以按照顺序取,也可以指定取。

什么是映射(Map)

集合中的每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复。 数据结构 键值对

类集框架主体结构

interface Iterator Collection ListIterator List Set Map LinkeList ArrayList HashSet SortedSet HashMap SortedMap LinkedHashSet TreeSet LinkedHashMap TreeMap Comparable Comparator Collections Arrays

//arrayList默认10,可无限长,关于泛型

代码语言:javascript
复制
public class Test{
 public static void main(String args[]){

  //ArrayList arrayList = new ArrayList();
  ArrayList<String> arrayList = new ArrayList<String>();

  arrayList.add("a");
  arrayList.add("b");
  arrayList.add("c");

  //String s = arrayList.get(1);
  //System.out.println(s);

   for(int i=0;i<3;i++){
    String s = arrayList.get(i);
    System.out.println(s);
   }

  }
}

优化

public class Test{
 public static void main(String args[]){
  ArrayList<String> arrayList = new ArrayList<String>();
  
  arrayList.add("a");
  arrayList.add("b");
  arrayList.add("c");
  arrayList.add("d");
  
  for(int i = 0; i<arrayList.size();i++){
   String s = arrayList.get(i);
   System.out.println(s);
  }
 }
}

类集框架 集合 无序 不可重复 列表 有序 可重复 映射

Collection和Iterator接口 Set与HashSet的使用方法

Collection 接口(一)

boolean add(Object o) 向集合当中加入一个对象 void clear() 删除集合当中的所有对象 boolean isEmpty() 判断集合是否为空 remove(Object o) 从集合中删除一个对象的引用 int size() 返回集合中元素的数目

Set继承了Collection

代码语言:javascript
复制
public class Test{
  public static void main(String args[]){

   //HashSet<String> hashSet = new HashSet<String>();
   //Set<String> set = new HashSet<String>();
   
  //别管就是转,方便
   Set<String> set = new HashSet<String>();
   set.add("a");
   set.add("b");
   set.add("c");
   set.add("d");

   int i = set.size();
  
   System.out.println(i);

  }
}

不可以重复

public class Test{
  public static void main(String args[]){

   //HashSet<String> hashSet = new HashSet<String>();
   //Set<String> set = new HashSet<String>();
   
  //别管就是转,方便
   Set<String> set = new HashSet<String>();

   boolean b1 = set.isEmpty();
   System.out.println(b1);

   set.add("a");
   set.add("b");
   set.add("c");
   set.add("d");
   set.add("c");
    
   boolean b2 = set.isEmpty();
   System.out.println(b2);
   
   int i = set.size();
   
   System.out.println("clear之前的长度"+i);
 
   set.clear();
   
   int j = set.size();
 
   System.out.println(j);

  }
}

取数据,迭代  iterate器 (Iterator)

public class Test{
 public static void main(String args[]){
  //HashSet<String> hashSet = new HashSet<String>();
  //Set<String> set = hashSet;
  //Iterator <-- Collection <-- Set <-- HashSet
  //hasNext() next()

  Set<String> set = new HashSet<String>();
  
  set.add("a");
  set.add("b");
  set.add("c");
  set.add("d");
  set.add("c");
 
  Iterator<String> it = set.iterator();

   boolean b1 = it.hasNext();
   if(b1){
    String s = it.next();
    System.out.println(s);
   }

   boolean b2 = it.hasNext();
   if(b2){
    String s = it.next();
    System.out.println(s);
   }

  }
}

迭代器的使用
it.hasNext();
还有没有下一个元素,如果这个游标后面有元素就返回true,否则,false;

it.next();
返回游标所指位置的下一个元素,取出,用hasNext()看有没有,next取

优化
public class Test{
 public stattic void main(String args[]){
  Set<String> set = new HashSet<String>();
  
  set.add("a");
  set.add("b");
  set.add("c");
  set.add("d");
  set.add("c");

  Iterator<String> it = set.iterator();
 
  while(it.hasNext()){
   String s = it.next();
   System.out.println(s);
 }
 }
}

Map
什么是映射(Map)
映射中的每一个元素包含一个键对象和一个值对象,键不可以重复,值可以重复。

public class Test{
 public static void main(String args[]){
  HashMap<String,String> hasMap = new HashMap<String,String>();
  Map<String,String> map = hasMap;
 
  map.put("1","a");
  map.put("2","b");
  map.put("3","c");
  map.put("4","d");

  int i = map.size();
  System.out.println(i);
 }
}

public class Test{
 public static void main(String args[]){
  HashMap<String,String> hasMap = new HashMap<String,String>();
  Map<String,String> map = hasMap;
 
  map.put("1","a");
  map.put("2","b");
  map.put("3","c");
  map.put("4","d");
  map.put("3","e");

  int i = map.size();
  System.out.println(i);
  
  String s = map.get("3");
  System.out.println(ss);
 }
}

equals函数的作用
复写equals函数的方法


“==”操作符的作用
User u1 = new User();
User u2 = new User();
User u3 = u1;

equals比较两个对象的内容是否相等

什么是对象的内容相等

对象的内容相等需要符合两个条件: 1.对象的类型相同(可以使用instanceof操作符进行比较); 2.两个对象的成员变量的值完全相同;

代码语言:javascript
复制
class Test{
 public static void main(String[] args){
  User u1 = new User();
  User u2 = new User();
  
  boolean b1 = u1.equals(u2);
  System.out.pritln(b1);
 }
}

作者Info:

【作者】:Jeskson

【原创公众号】:达达前端小酒馆。

【转载说明】:转载请说明出处,谢谢合作!~

若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-02-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 字面量:
  • 什么是面向对象?
  • 应该如何学习面向对象?
  • 什么是面向对象思维方法?
  • 创建类的方法
  • 创建对象的方法
  • 生成对象的方法
  • 函数的重载和构造函数的作用
  • 什么叫函数的重载呢?
  • 什么是构造函数?
  • 继承,封装,多态
  • 什么是继承?
  • 函数的复写(override),也称为覆盖或者重写
  • 什么是复写?
  • 抽象类?
  • 什么是抽象函数?
  • 什么是抽象类?
  • 包和访问权限的语法
  • 什么是接口(interface)
  • 接口的基本语法如上
  • Java当中的IO
  • 装饰者模式
  • BufferedReader介绍
  • BufferedReader全称字符输入处理流
  • 线程的简单控制方法
  • 类集框架
  • 什么是集合(Set)
  • 什么是列表(List)
  • 什么是映射(Map)
  • 类集框架主体结构
  • Collection 接口(一)
  • 什么是对象的内容相等
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档