Array及其用法 # arraycopy(Object src,int start1,Object des,int start2,int length); methods array to copy array(两个起点一个长度,前copy from,后copy to) # int(Object/String/Char)[] = new int[6](...); # array[0]=1; set Methods # int a = array[0];get Methods # int[] a = new int[]{1,2,3}; initials arrays
ArrayList及其方法 # ArrayList<Object> list = new ArrayList<Object>(); # list.add(new Object()); set Methods # list.size(); return int # list.contains(obj); return boolean # list.indexof(obj); return index if there is no obj return -1; # list.isEmpty(); return boolean # list.remove(); # list.get(4); get Methods
Array and Array Difference ## An array needs to know its size at time of creation, whereas an ArrayListdoes not: # new String[6]; # new ArrayList<E>();
## To assign an object in a regular array, you must assign it to a specific index. # myList[4] = b; myArrayList.add(b);
## charAt(int index) return character ## indexof(char '') ## indexof(string "") ## length() ## lastindexof(char '') ## lastindexof(string "") ## boolen equals(obj) ## int compareTo(obj) return -1 or 0 or 1 ## substring(int index1,int index2) excluding index2 substring(index) from index to end ## "".concat("") ## toUperCase()/toLowerCase() ## toString() ## split(":") return String[] ## void getChars(i,j,A,k): returns characters from i to j (excluding), and stores them into array A starting from A[k]. ## replace(old,new) ## StringTokenizer(s,"nu",true) new mystr = new StringTokenizer("23 34 34"," ",true); mystr.hasMoreTokens() mystr.nextToken() 注意两个方法
int[][] array =newint[][]{{1,2,3},{1,2,3},{1,2,3,4}}; //use for each System.out.println("======with for each first======"); for(int[] i :array){ for(int j=0;j<i.length;j++) { System.out.print(i[j]+" "); } System.out.println(); }
System.out.println("========with for each second======="); System.out.println("array.length:"+array.length); for(int i=0;i<array.length;i++){ for(int j :array[i]){ System.out.print(j+" "); } System.out.println(); }
System.out.println("========with two for loop======="); for(int i=0;i<array.length;i++) { for(int j=0;j<array[i].length;j++) { System.out.print(array[i][j]+" "); } System.out.println(); }
System.out.println("========with two for each======="); for(int[]i:array) { for(int j:i){ System.out.print(j+" "); } System.out.println(); }
# • Math Class: – Doesn’t have instance variables. – Can’t make an instance of class.
## methods # Math.random()--->return a double between 0.0-1.0(include) # Math.round(float a) ---->return a round int # Math.abs(double a)---->return a absolute double # Math.max(int a,int b) # Math.min(int a,int b)
关于static 和 final
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//when use static only it can be autometic initialised,when classloading publicstaticint a;
//when use final only it must be initialised when declared or in the constructor publicclasstest{ publicfinalint a; test() {a=0;} }
//when use final and static it must initialised when declared,but not allowed in the constructor because in the static valiables are created when classloading,but constructor is invoked when the instance is created publicstaticfinalinta=0;
Object 类提供的API 方法
1 2 3 4
# .equals() # .toString() # .hasCode() return a object unique ID according to it's memory address # .getClass() return CLass of the object
/*interface is 100% abstract *abstract 是一个class(含有至少有一个abstract method) *interface can extants more than one interfaces,but it neither can extant *from superclass *a subclass just can extant one superclass,but can implement more than one *interfance *interfance and abstract both hava not instances. *interface 中的的methods are all public whatever it declare as plubic. */
/* *在接口中声明的变量必须赋初值,(接口默认所有的变量常量==>public static final int d=10 /int d =10) */ /*下面的例子声明 public interface test { final static int d = 10; static final int a =10; static int r =1; final int s = 3; public int f =4; int g =4; public static final int o =9; public final static int h =8; } */
//在interface中我们可以使用default在接口中实现了一个默认方法和static 在接口中可以来实现一个静态方法 //default methods can be overriden in the instance of the interface //static methods can not be overriden in the instance of interface //在interface 中如果含有static methods,可以直接使用 接口名.static方法名来访问该方法 //在java 接口中,当两个接口都定义了相同的默认方法时,实现类必须使用super来区分 //as TestInterface1.super.show(),TestInterface2.show()
//local variable on the stack //instance varialbe on the heap local variables include the values in methods and the parameters and the referance variables; in the stack,stack will keep the methods state,the executing methods will be on the top of stack; instance variables include the variables in classbut outside methods,objects that referance variables refer to;
//方法,方法的参数,方法里面的成员变量,形参(referance variables)都在盏中 //类,类的成员变量(在类中,除了方法外的变量),类的实例放在堆中 //常量池中储存的是final定义的常量,和Stirng双引号内的数据(String a = "123")
## an Object that signals to the calling code,the occurence of an unusual condotion;
what the difference between the private ,public ,protected access modifiers
1 2 3 4 5 6 7 8 9 10 11 12 13 14
## public: interface access disadvange: we have little control over how it is accessed ## protected: Package member and classes which extends it; ## default: Package members(defalut is achieved by ot adding a qualifier) ## private: class only advange: we have complete control over how the data member is accessed
• public – public instance variables and methods are inherited • protected – protected instance variables and methods are inherited • private – any private instance variables and methods are not inherited and cannot be seen by the subclass
what is Object
1
## An object is an instance of a particular class.
what is class
1 2 3 4
## the class defines attributes and operations exposed by one or more related object ## An object is defined by a class. ## In Java, a class is to: # – define a kind of object or in other words, to define a data type
what is Polymorphism
1 2 3 4 5 6 7 8 9
## Polymorphism → Using a single definition (superclass) with different types (subclass).
## about Interface Java’s “multiple inheritance” is at interface level only! An interface allows polymorphic capabilities without the problems of multiple inheritance.
## how interface implements mutiple ingeritance Since an interface has NO implemented methods, multiple inheritance is not a problem, as no class inherits a "finished" method.
what is method overriding
1 2 3 4
## we redefined the inherited methods • Subclass can redefine a superclass method – When the method is mentioned in the subclass, then the subclass method version is used. – Can access the original superclass method with super.methodName
What difference between pass by value and pass by
1
## pass-by-value == pass by copy
What is constructor
1
## A constructor is a special method with same name as the class name,used for initialisation.
instance variables will be initialised with defaut 0,but local variables will not be initialised;
What is the difference between the this() and super()
1 2 3 4 5 6 7 8 9 10 11
this. vs super. ## this used to access the class itself‘s variables and methods ## super used to access the superclass's valriables and methods
this() vs supser() ## this() used to call the class constructor ## super() used to call the superClass constructor when in the subclass, we want to call superClass's constructor we use super(); when int the subclass, we want to cal class's constructor or in same class we want to call a constructor from another overload one we use this()(maybe with arguments);
# A constructor can have a call to either super() or this() but not toboth!
what is downcasting
1 2
## Covert SuperClass reference to a subClass reference # Can only be done when superclass reference is actually referring to a subclass object.
what the defference between array and ArrrayList
1 2
## ArrayList can vary in length,but array can not ## ArrayList have a richer API than array
what is javadoc
1
## javadoc is a fomal description(or API) of your java code;it means someone who want to use your code no need to read your code,can understand what your code does;
Javadoc tag
1 2 3 4 5 6 7
生成指定文件的javadoc ## javadoc -d docs Examples.java
## @author (classes and interfaces only,required) ## @version(classes and interfaces only,required) ## @param(methods and constructors only) ## @return(methods only)
recursive
1 2 3
包含两个部分 ## base case ## recursive call
three main concepts about creating GUI
1 2 3 4 5
## component: An object user can see on the screen and can interact with ## container: A component that can hold other components ## Event:An action trigger by the user
Desigining a GUI involves creating components and add components into containers,and arranging for the program to respond to the events