Java基础
1.冒泡排序
public static void main ( String [ ] args) { int [ ] intAr = { 3 , 2 , 1 , 5 , 6 , 4 , 2 , 1 , 8 } ; bubbleSort ( intAr) ; System . out. println ( getArrStr ( intAr) ) ;
} public static int [ ] bubbleSort ( int [ ] intArr) { for ( int i = 0 ; i < intArr. length - 1 ; i++ ) { for ( int j = 0 ; j < intArr. length - i - 1 ; j++ ) { if ( intArr[ j] > intArr[ j + 1 ] ) { int tmp = intArr[ j + 1 ] ; intArr[ j + 1 ] = intArr[ j] ; intArr[ j] = tmp; } } } return intArr;
}
2.二分查找
public static void main ( String [ ] args) { int [ ] intAr = { 3 , 2 , 1 , 5 , 6 , 4 , 2 , 1 , 8 } ; int index = binarySearch ( intAr, 0 ) ; System . out. println ( "获取到下标:" + index) ; }
public static int binarySearch ( int [ ] intArr, int search) { int startIndex = 0 ; int endIndex = intArr. length - 1 ; while ( startIndex <= endIndex) { int mid = ( startIndex + endIndex) / 2 ; if ( intArr[ mid] == search) { return mid; } else if ( intArr[ mid] < search) { startIndex = mid + 1 ; } else { endIndex = mid - 1 ; } } return - 1 ;
}
3. class(类)
public static void main ( String [ ] args) { Person person = new Person ( ) ; person. name = "许康杰" ; person. age = 20 ; System . out. println ( person. name) ; System . out. println ( person. age) ; System . out. println ( person. eat ( "金针菇" ) ) ; person. play ( ) ; Person person2 = new Person ( ) ; person2. name = "李国涛" ; person2. age = 18 ; System . out. println ( person2. name) ; System . out. println ( person2. age) ; Person person3 = person2; System . out. println ( person3. name) ; System . out. println ( person3. age) ; useVar ( ) ; } public static void useVar ( ) { { int var = 10 ;
System . out. println ( var ) ; } } Person 类:public class Person { String name; int age; double high; double weight; String gender; public String eat ( String food) { System . out. println ( food+ "被消化了一部分" ) ; return "消化过的食物" ; } public void drink ( String drink) { System . out. println ( "人类可以喝饮料..." ) ; } public void play ( ) { System . out. println ( "人类可以玩游戏..." ) ; } }
4.参数传递
public static void main ( String [ ] args) { Person person = new Person ( ) ; person. name = "易政" ; usePerson ( person) ; System . out. println ( person. name) ; System . out. println ( person. age) ; usePerson ( new Person ( ) ) ; } public static void usePerson ( Person inPerson) { System . out. println ( inPerson) ; inPerson. name = "汪雨" ; inPerson. age = 18 ;
}
5.封装
public static void main ( String [ ] args) { Student student = new Student ( ) ; student. setName ( "李四" ) ; student. setAge ( 18 ) ; student. setGender ( "不知道" ) ; student. printInfo ( ) ; System . out. println ( student. getName ( ) ) ; System . out. println ( student. getAge ( ) ) ; Phone phone = new Phone ( ) ; phone. setNumber ( "183****8888" ) ; phone. setUser ( "王五" ) ; System . out. println ( phone. getUser ( ) ) ; System . out. println ( phone. getNumber ( ) ) ; } Student 类:public class Student { private String name; private int age; private String gender; public void setName ( String inName) { name = inName; } public void setAge ( int age) { this . age = age; } public void setGender ( String inGender) { if ( inGender. equals ( "男" ) || inGender. equals ( "女" ) ) { gender = inGender; } else { System . out. println ( "性别只能为男或女,您输入的信息不正确..." ) ; } } public int getAge ( ) { return age; } public String getName ( ) { return name; } public String getGender ( ) { return gender; } public void printInfo ( ) { System . out. println ( "我叫:" + name + " 今年:" + age + " 性别是:" + gender) ; } }
6.构造方法
public static void main ( String [ ] args) { Phone phone = new Phone ( ) ; phone. setNumber ( "183****8888" ) ; phone. setUser ( "陆玉龙" ) ; System . out. println ( phone. getUser ( ) ) ; System . out. println ( phone. getNumber ( ) ) ; Phone phone2 = new Phone ( "袁枫" , "166****6666" ) ; System . out. println ( phone2. getUser ( ) ) ; System . out. println ( phone2. getNumber ( ) ) ; Phone phone3 = new Phone ( "汪雨" ) ; System . out. println ( phone3. getUser ( ) ) ; System . out. println ( phone3. getNumber ( ) ) ; phone2. call ( "188****8888" ) ; Phone phone4 = new Phone ( "袁枫女朋友" , "188****8888" ) ; phone2. call ( phone4) ; useStaticPhone ( phone2) ; } public void usePhone ( Phone phone) { System . out. println ( "手机号:" + phone. getNumber ( ) + "正在被使用..." ) ; } public static void useStaticPhone ( Phone phone) { System . out. println ( "Demo05中的代码:" + "手机号:" + phone. getNumber ( ) + "正在被使用..." ) ; } phone类:public class Phone { public Phone ( ) { System . out. println ( "这是Phone的构造方法..." ) ; } public Phone ( String user, String number) { this . user = user; this . number = number; System . out. println ( "这是两个参数的Phone的构造方法..." ) ; } public Phone ( String user) { this ( user, "111****0000" ) ; System . out. println ( "这是1个参数的Phone的构造方法..." ) ;
} private String user; private String number; public String getUser ( ) { return user; } public void setUser ( String user) { this . user = user; } public String getNumber ( ) { return number; } public void setNumber ( String number) { this . number = number; } public void call ( String number) { System . out. println ( this . number + "正在打电话给" + number) ; } public void call ( Phone phone) { System . out. println ( this . user + "\t" + this . number + "正在打电话给" + phone. user + "\t" + phone. number) ; } public void show ( ) { System . out. println ( "当前用户为:" + user+ "其手机号码为:" + number) ; } }
7.成员方法
public class Demo06UseObjectFunction { public static void main ( String [ ] args) { Phone phone2 = new Phone ( "袁枫" , "166****6666" ) ; System . out. println ( phone2. getUser ( ) ) ; System . out. println ( phone2. getNumber ( ) ) ; Demo06UseObjectFunction demo06UseObjectFunction = new Demo06UseObjectFunction ( ) ; demo06UseObjectFunction. usePhone ( phone2) ; useStaticPhone ( phone2) ; Demo05GetObject . useStaticPhone ( phone2) ; demo06UseObjectFunction. useStaticPhone ( phone2) ; phone2. show ( ) ; } public void usePhone ( Phone phone) { System . out. println ( "手机号:" + phone. getNumber ( ) + "正在被使用..." ) ;
} public static void useStaticPhone ( Phone phone) {
System . out. println ( "Demo06中的代码:" + "手机号:" + phone. getNumber ( ) + "正在被使用..." ) ; } }
8. static
package com. shujia. Day04_packaging ; public class Demo07StaticVar { static int value; static int data = 20 ; int var = 1 ; public static void main ( String [ ] args) { Demo07StaticVar . value = 10 ; System . out. println ( value) ; System . out. println ( data) ; useVar ( ) ;
new Demo07StaticVar ( ) . objectVar ( ) ; Per per1 = new Per ( "张三" , "美国" ) ; per1. show ( ) ; Per per2 = new Per ( "李四" , "中国" ) ; per2. show ( ) ; per1. show ( ) ; } public static void useVar ( ) {
System . out. println ( "获取到Value:" + value) ; System . out. println ( "获取到data:" + data) ; } public void objectVar ( ) { var = 10 ; data = 30 ; System . out. println ( "当前data:" + data) ; System . out. println ( "当前var:" + var ) ; } } Per 类:public class Per { String name; static String nationality; public Per ( ) { } public Per ( String name, String nationality) { this . name = name; this . nationality = nationality; } public void show ( ) { System . out. println ( "姓名:" + name + "\t" + "国籍:" + nationality) ; }
}
9. main方法
public class Demo08Main { public static void main ( String [ ] args) { int length = args. length; System . out. println ( length) ; if ( length > 0 ) { for ( String arg : args) { System . out. println ( "argument:" + arg) ; } } }
} public class Demo08UseMain { public static void main ( String [ ] args) { String [ ] arguments = { "1" , "2" } ; Demo08Main . main ( arguments) ; }
}