arraylist 构造和添加
import java.util.ArrayList;
public class helloworld {
public static void main(String[] args) {
//创建空的集合对象
ArrayList<String> array = new ArrayList<String>();
//将指定元素添加到集合的末尾
array.add("hello");
array.add("world");
//在集合的指定位置添加元素
array.add(1, "java");
System.out.println("array:" + array); //array:[hello, java, world]
}
}
arraylist 常用方法
import java.util.ArrayList;
public class helloworld {
public static void main(String[] args) {
//创建空的集合对象
ArrayList<String> array = new ArrayList<String>();
//将指定元素添加到集合的末尾
array.add("hello");
array.add("world");
//删除指定元素,返回为boolean类型
// System.out.println(array.remove("hello"));
//删除指定索引,返回字符串类型
// System.out.println(array.remove(0));
//修改指定索引处的元素
// System.out.println(array.set(0,"hel"));
//返回指定索引处的元素,返回被修改的元素
System.out.println(array.get(0));
//返回集合元素的个数
System.out.println(array.size());
System.out.println("array:" + array);
}
}
集合存储字符串对象并遍历
import java.util.ArrayList;
public class helloworld {
public static void main(String[] args) {
//创建空的集合对象
ArrayList<String> array = new ArrayList<String>();
//将指定元素添加到集合的末尾
array.add("hello");
array.add("world");
array.add("java");
for (int i = 0; i < array.size(); i++) {
String s = array.get(i);
System.out.println(s);
}
}
}
arraylist 存储学生对象并遍历
public class Student {
// 成员变量
private String name;
private int age;
// 构造方法 无参
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// 成员方法
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void show() {
System.out.println(name + "," + age);
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class StudentDemo {
public static void main(String[] args) {
//创建集合
ArrayList<Student> array = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("xiaoming",30);
Student s2 =new Student("meimei",20);
Student s3= new Student("huahua",25);
//添加学生对象到集合中
array.add(s1);
array.add(s2);
array.add(s3);
//遍历集合
for (int i=0;i<array.size();i++){
Student s = array.get(i);
System.out.println(s.getName()+s.getAge());
}
}
}
ArrayList存储学生对象并遍历,从键盘录入
public class Student {
// 成员变量
private String name;
private String age;
// 构造方法 无参
public Student() {
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
// 成员方法
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(String age) {
this.age = age;
}
public String getAge() {
return age;
}
public void show() {
System.out.println(name + "," + age);
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class StudentDemo {
public static void main(String[] args) {
//创建集合
ArrayList<Student> array = new ArrayList<Student>();
/*//键盘录入
Scanner sc =new Scanner(System.in);
System.out.println("请输入姓名");
String name = sc.nextLine();
System.out.println("请输入学生年龄");
String age = sc.nextLine();
//创建学生对象
Student s = new Student();
s.setName(name);
s.setAge(age);
//添加学生对象到集合中
array.add(s);*/
addStudent(array);
addStudent(array);
addStudent(array);
//遍历集合
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getName() + s.getAge());
}
}
//用方法提高代码的复用性
public static void addStudent(ArrayList<Student> array) {
//键盘录入
Scanner sc = new Scanner(System.in);
System.out.println("请输入姓名");
String name = sc.nextLine();
System.out.println("请输入学生年龄");
String age = sc.nextLine();
//创建学生对象
Student s = new Student();
s.setName(name);
s.setAge(age);
//添加学生对象到集合中
array.add(s);
}
}
继承 super this的使用
public class zi extends fu {
public int age = 30;
public void show(){
int age = 20;
System.out.println(age);
System.out.println(this.age);
System.out.println(super.age);
}
}
public class zi extends fu {
public int age = 30;
public void show(){
int age = 20;
System.out.println(age);
System.out.println(this.age);
System.out.println(super.age);
}
}
public class demo {
public static void main(String[] args) {
zi z = new zi();
z.show();
z.showFu();
}
}
方法重写
父类的私有方法不能被重写
子类权限方法不能更低
public class phone {
public void call(String name) {
System.out.println("给" + name + "打电话吧");
}
}
public class newPhone extends phone {
@Override //方法重写
public void call(String name) {
System.out.println("开启视频");
// System.out.println("给"+name+"打电话吧");
super.call(name);
}
}
public class phoneDemo {
public static void main(String[] args) {
phone p = new phone();
p.call("xiaoming");
newPhone np = new newPhone();
np.call("xiaoming2");
}
}
学生老师类无参带参构造方法继承使用
public class Person {
private String name;
private int age;
//alt+insert 添加无参构造方法
public Person() {
}
//alt+insert 添加带参构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//alt+insert getter and setter 生成get set方法
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 class Teacher extends Person{
public Teacher(){
}
public Teacher(String name,int age){
// this.name =name;
// this.age =age;
super(name,age);
}
public void teach(){
System.out.println("教师教书");
}
}
public class PersonDemo {
public static void main(String[] args) {
//创建对象并测试--无参构造方法
Teacher t1 = new Teacher();
t1.setName("xiaoming");
t1.setAge(30);
t1.teach();
System.out.println(t1.getName() + "," + t1.getAge());
//带参构造方法
Teacher t2=new Teacher("xiaom2",20);
t2.teach();
System.out.println(t2.getName()+","+t2.getAge());
}
}
猫和狗继承animal
package pack;
public class Animal {
private String name;
private int age;
public Animal() {
}
public Animal(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;
}
}
package pack;
public class Cat extends Animal{
public Cat() {
}
public Cat(String name, int age) {
super(name, age);
}
public void CatchMouse(){
System.out.println("猫抓老鼠");
}
}
package pack;
public class Dog extends Animal{
public Dog() {
}
public Dog(String name, int age) {
super(name, age);
}
public void LookDoor(){
System.out.println("狗看门");
}
}
package pack;
public class AnimalDemo {
public static void main(String[] args) {
//创建猫类对象并测试
Cat c1=new Cat();
c1.setName("mao1");
c1.setAge(1);
System.out.println(c1.getName()+","+c1.getAge());
c1.CatchMouse();
Cat c2 = new Cat("mao2",2);
System.out.println(c2.getName()+","+c2.getAge());
c2.CatchMouse();
}
}
final 最终的意思
修饰方法,表明该方法是最终方法,不能被重写
修饰变量,表明该变量是常量,不能再次被赋值
修饰类,表明该类是最终类,不能被继承
变量是基本类型:final修饰指的是基本类型的数据值不能改变
变量是引用类型:final修饰指的是引用类型的地址值不能发生改变,但是地址里面的内容是可以发生改变的
static 静态,可修饰成员方法和成员变量
特点:被类的所有对象共享
可通过类名调用,推荐;也可通过对象调用
public class student {
// 成员变量
public String name;
public int age;
public static String university;
// 成员方法
public void show() {
System.out.println(name + "," + age+","+university);
}
}
public class studentDemo {
public static void main(String[] args) {
// 创建对象
student.university = "daxue";
student s1 = new student();
s1.name = "xiaoming";
s1.age =20;
s1.show();
student s2 = new student();
s2.name = "xiaoming2";
s2.age =21;
s2.show();
}
}
static 静态成员方法只能访问静态成员
内部类
/*内部类可以直接访问外部类的成员,包括私有
* 外部类要访问内部类的成员,必须创建对象*/
public class Outer {
private int num = 10;
public class Inner(){
public void show(){ //成员内部类
System.out.println(num);
}
}
public void method(){
// show();
Inner i = new Inner();
i.show();
}
}
System的使用
public class demo {
public static void main(String[] args) {
System.out.println("开始");
//终止当前运行的虚拟机,非零表示异常终止
// System.exit(0);
System.out.println("结束");
//返回当前时间,以毫秒为单位
System.out.println(System.currentTimeMillis() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年"); //转换为年
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("运行时间为:" + (end - start) + "毫秒");
}
}
object类中的toString方法
package heima02;
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;
}
//alt+insert 选择toString()--重写了object中的toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s = new Student();
s.setName("xiao");
s.setAge(20);
System.out.println(s); //heima02.Student@4b67cf4d
System.out.println(s.toString()); //heima02.Student@4b67cf4d
}
}
object类中的equals方法
package heima02;
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;
}
//alt+insert 选择toString()--重写了object中的toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//重写equals方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
}
package heima02;
public class ObjectDemo {
public static void main(String[] args) {
Student s = new Student();
s.setName("xiao");
s.setAge(20);
Student s2 = new Student();
s2.setName("xiao");
s2.setAge(20);
//比较两个对象内容是否相等
System.out.println(s.equals(s2));
/* public boolean equals(Object obj) {
this----s
obj----s2
没有在Student中重写equals方法,指的是地址相比较,需要重写equals方法则是比较地址
return (this == obj);
}*/
}
}
冒泡排序代码实现
package heima02;
public class ArrayDemo {
public static void main(String[] args) {
int [] array = {2,9,7,4,6,5,1};
System.out.println("排序前"+arrayToString(array));
for(int x = 0;x<array.length-1;x++) { //总共比较了多少次
for (int i = 0; i < array.length - 1 - x; i++) {//每轮进行数据两两比较
if (array[i] > array[i + 1]) {
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
System.out.println("排序后"+arrayToString(array));
}
//把数组中的元素按照指定的规则组成一个字符串
public static String arrayToString(int[] arr){
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i=0;i< arr.length;i++){
if(i==arr.length-1){
sb.append(arr[i]);
}else {
sb.append(arr[i]).append(",");
}
}
sb.append("]");
String s =sb.toString();
return s;
}
}
array排序
package heima02;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
int [] array = {2,9,7,4,6,5,1};
System.out.println("排序前"+ Arrays.toString(array));
Arrays.sort(array);
System.out.println("排序后"+ Arrays.toString(array));
}
//排序前[2, 9, 7, 4, 6, 5, 1]
//排序后[1, 2, 4, 5, 6, 7, 9]
}
int 和string的相互转换
public class ArrayDemo {
public static void main(String[] args) {
//int----string
int num = 100;
//方式一
String s1= ""+num;
System.out.println(s1);
//方式二
String s2 = String.valueOf(num);
System.out.println(s2);
//string ---int
String s="20";
//方式一 string-integer-int
Integer i =Integer.valueOf(s);
int x = i.intValue();
System.out.println(x);
//方式一 string-int
int y =Integer.parseInt(s);
System.out.println(y);
}
}
字符串中数据排序
package heima02;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
//定义一个字符串
String s = "23 34 7 65 45 ";
//把字符串中的数据存储到int类型数组中
String[] strArray = s.split(" ");
//定义一个int数组,把string[]数组中的元素存储到int数组中
int[] arr = new int[strArray.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(strArray[i]);
}
/*for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}*/
//对int数组进行排序
Arrays.sort(arr);
//把排序后的数组拼接到字符串中,
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i]).append(" ");
}
}
String ss = sb.toString();
//输出结果
System.out.println("result:" + ss);
}
}
try catch 异常处理
public class demo {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method(){
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
}catch (Exception e){
e.printStackTrace();
}
}
}
throws 异常处理,抛出异常,程序中断
public class demo {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method() throws ArrayIndexOutOfBoundsException{
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
}
}