數組:
int[] num=new int[3];//聲明num變量在棧內存里,new是在堆內存中給對象分配了空間
for(int i=0;i<num.length;i++)
{
System.out.println(num[i]);
}
--------------------------------------------------------------
class Stringtest
{
public static void main(String[] args)
{
Student[] students;
students=new Student[3];
students[0]=new Student("lisi",18);
for(int i=0;i<students.length;i++)
{
System.out.println(students[i]);
}
class Student
{
String name;
int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
}
數組的相關(guān)操作:
在Java中,所有的數組都有一個(gè)缺省的屬性length,用于獲取數組中元素的個(gè)數
數組的復制:System.arraycopy()
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
數組的排序:Arrays.sort()
在java.util包中。
在已排序的數組中查找某個(gè)元素:Arrays.binarySearch(),返回值為int型,是要查找的數組的索引
/*int[] num=new int[]{3,1,2};
Arrays.sort(num);
int index=Arrays.binarySearch(num,3);*/
如果是對對象排序的話(huà)sort(Object[] a),All elements in the array must implement the Comparable interface,接口Comparable在java.lang包下
---------------------------------------------------------------------------
import java.util.Arrays;
class ArrayTest1
{
public static void main(String[] args)
{
Student[] ss=new Student[]{new Student("zs",18),
new Student("ls",19),
new Student("ww",20)};
Arrays.sort(ss);
int index=Arrays.binarySearch(ss,new Student("ls",19));
System.out.println("index="+index);
if (index > -1)
{
System.out.println(ss[index]);
}
else
System.out.println("Not Exists");
}
}
class Student implements Comparable
{
int num;
String name;
Student(String name,int num)
{
this.num=num;
this.name=name;
}
public String toString()
{
return "num="+num+","+"name="+name;
}
public int compareTo(Object o)
{
Student s=(Student)o;
//int retsult=num>s.num ? 1 : (num==s.num ? 0 : -1);這是第一種比較方法,下面定義了第二種比較方法
int result=num>s.num ? 1 : (num==s.num ? 0 : -1);
if (result==0)
{
result=name.compareTo(s.name);
}
return result;
}
}
------------------------------------------------------------------------------------
main函數:
public static void main(String[] args)//String是除了那8中以外的類(lèi)型,所以是引用數組,一開(kāi)始args并沒(méi)有被分配空間,它是用來(lái)接受命令行參數的。比如在命令行里輸入java calssname OtherWords,那么在args里就會(huì )保存OtherWords的內容
注:數組本身就是引用類(lèi)型
如何交換int x,int y的值,而不利用第三個(gè)變量:x=x+y;y=x-y;x=x-y;
函數的調用:在Java中,傳參時(shí),都是以傳值的方法進(jìn)行。對于基本數據類(lèi)型,傳遞的數組的拷貝;對于引用類(lèi)型,傳遞的是引用的拷貝
在打印(System.out.println)一個(gè)對象的時(shí)候,會(huì )自動(dòng)調用這個(gè)對象的toString()方法,原Object類(lèi)中的toString()方法Returns a string representation of the object.It is recommended that all subclasses override this method.
對象的克隆:
為了獲取對象的一份拷貝,我們可以利用Object類(lèi)的clone()方法
在派生類(lèi)中覆蓋基類(lèi)的clone()方法,并聲明為public(因為在Object類(lèi)中該方法是protected)
在派生類(lèi)的clone()方法中,調用super.clone()。在運行時(shí)刻,Object中的clone()識別出你要復制的是哪一個(gè)對象,然后為此對象分配空間,并進(jìn)行對象的復制,將原始對象的內容一一復制到新對象的存儲空間中
在派生類(lèi)中實(shí)現Cloneable接口(沒(méi)有任何抽象方法,只是為了告訴編譯器這個(gè)對象可以克?。?,否則調用clone()方法會(huì )拋出CloneNotSupportedException異常
如果被克隆的對象的數據成員中含有對象,那么就需要作深層次的克隆才能將作為數據成員的對象克隆,否則是把對象變量的值進(jìn)行了拷貝,即只是把作為數據成員的對象的引用(地址)進(jìn)行了拷貝。作深層次的克隆,就是要讓作為數據成員的對象所屬的類(lèi)也覆蓋clone()方法,并且實(shí)現Cloneable接口(與被克隆的對象所屬的類(lèi)中的實(shí)現方法同)
class Student implements Cloneable
{
String name;
int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
public Object clone()
{
Object o=null;
try
{
o=super.clone();//super.clone()返回的是Object類(lèi),根據需要可進(jìn)行強制類(lèi)型轉換
}
catch(CloneNotSupportedException e)
{
System.out.println(e.toString());
}
return o;
}
}
封裝類(lèi):(有時(shí)函數參數是引用類(lèi)型,但我們又要傳遞基本數據類(lèi)型)
針對八種基本數據類(lèi)型定義的相應的引用類(lèi)型——封裝類(lèi)(在java.lang包中定義的,基本數據類(lèi)型與封裝類(lèi)建立對應關(guān)系之后,封裝類(lèi)就是只讀類(lèi),不能改變)
基本數據類(lèi)型 封裝類(lèi)
boolean Boolean
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
--------------------------------------------------------------------
class Test
{
public static void main(String[] args)
{
int i=3;
Integer in=new Integer(i);//Integer的一個(gè)構造函數Integer(int value)
int j=in.intValue();//int intValue() Returns the value of this Integer as an int
System.out.println("j="+j);
String str=in.toString();//String toString() Returns a String object representing this Integer‘s value.
System.out.println("str="+str);
String str1="134";//String要是數字類(lèi)型的
System.out.println(Integer.valueOf(str1));//static Integer valueOf(String s) Returns an Integer object holding the value of the specified String.
//static int parseInt(String s) Parses the string argument as a signed decimal integer.
}
}
Class類(lèi)(在java.lang包中,Instances of the class Class represent classes and interfaces in a running Java application):
在Java中,每個(gè)class都有一個(gè)相應的Class對象。也就是說(shuō),當我們編寫(xiě)一個(gè)類(lèi),編譯完成后,在生成的.class文件中,就會(huì )產(chǎn)生一個(gè)Class對象,用于表示這個(gè)類(lèi)的類(lèi)型信息
獲取Class實(shí)例的三種方式:
(1)利用對象調用getClass()方法獲取該對象的Class實(shí)例;
(2)使用Class類(lèi)的靜態(tài)方法forName(),用類(lèi)的名字獲取一個(gè)Class實(shí)例(static Class forName(String className) Returns the Class object associated with the class or interface with the given string name. );
(3)運用.class的方式來(lái)獲取Class實(shí)例,對于基本數據類(lèi)型的封裝類(lèi),還可以采用.TYPE來(lái)獲取相對應的基本數據類(lèi)型的Class實(shí)例
在newInstance()調用類(lèi)中缺省的構造方法 Object newInstance()(可在不知該類(lèi)的名字的時(shí)候,常見(jiàn)這個(gè)類(lèi)的實(shí)例) Creates a new instance of the class represented by this Class object.
在運行期間,如果我們要產(chǎn)生某個(gè)類(lèi)的對象,Java虛擬機(JVM)會(huì )檢查該類(lèi)型的Class對象是否已被加載。如果沒(méi)有被加載,JVM會(huì )根據類(lèi)的名稱(chēng)找到.class文件并加載它。一旦某個(gè)類(lèi)型的Class對象已被加載到內存,就可以用它來(lái)產(chǎn)生該類(lèi)型的所有對象
----------------------------------------------------------------------------------------------------
//獲取Class實(shí)例的三種方式:
class ClassTest
{
public static void main(String[] args)
{
Point pt=new Point();
Class c1=pt.getClass();
System.out.println(c1.getName());// String getName() Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
try
{
Class c2=Class.forName("Point");//static Class forName(String className) throws ClassNotFoundException Returns the Class object associated with the class or interface with the given string name.
System.out.println(c2.getName());
}
catch(Exception e)
{
e.printStackTrace();
}
Class c3=Point.class;
System.out.println(c3.getName());
Class c4=int.class;
System.out.println(c4.getName());
Class c5=Integer.TYPE;
System.out.println(c5.getName());
Class c6=Integer.class;
System.out.println(c6.getName());
}
}
class Point
{
int x,y;
}
/*結果:
Point
Point
Point
int
int
java.lang.Integer*/
----------------------------------------------------------------------------
//newInstance()調用類(lèi)中缺省的構造方法:
class anClassTest
{
public static void main(String[] args)
{
if(args.length!=1)
{
return;
}
try
{
Class c=Class.forName(args[0]);
Point pt=(Point)c.newInstance();
pt.output();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class Point
{
int x,y;
void output()
{
System.out.println("x="+x+",y="+y);
}
}
class Line
{
int length,x,y;
void output()
{
System.out.println("length="+length+",x="+x+",y="+y);
}
}
?The Reflection API represents,or reflects,the classes,interfaces,and objects in the current Java Virtual Machine.在java.lang.reflect包里
Runtime類(lèi)(在java.lang包中定義)和Process類(lèi)
每一個(gè)Java程序都有一個(gè)Runtime類(lèi)的單一實(shí)例
通過(guò)Runtime.getRuntime()獲取Runtime類(lèi)的實(shí)例
Runtime類(lèi)是使用單例模式的一個(gè)例子
import java.io.*;
class RuntimeTest
{
public static void main(String[] args)
{
Runtime rt=Runtime.getRuntime();
System.out.println(rt.freeMemory());//long freeMemory():Returns the amount of free memory in the Java Virtual Machine.
System.out.println(rt.totalMemory());// long totalMemory():Returns the total amount of memory in the Java virtual machine.
try
{
rt.exec("notepad");//Process exec(String command):Executes the specified string command in a separate process.
Process p=rt.exec("java anClassTest Point");//現在p就表示java anClassTest這個(gè)子進(jìn)程。類(lèi)似這種可做圖形調用工具
InputStream is=p.getInputStream();
int data;
while((data=is.read())!=-1)
{
System.out.print((char)data);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
設計模式:在我們進(jìn)行程序設計時(shí),逐漸形成了一些典型問(wèn)題和問(wèn)題的解決方案,這就是軟件模式。每一個(gè)模式描述了一個(gè)在我們程序設計中經(jīng)常發(fā)生的問(wèn)題,以及該問(wèn)題的解決方案。當我們碰到模式所描述的問(wèn)題,就可以直接用相應的解決方法去解決這個(gè)問(wèn)題,這就是設計模式
單例模式:
(1)一個(gè)類(lèi)只有一個(gè)實(shí)例,而且自行實(shí)例化并向這個(gè)系統提供這個(gè)實(shí)例,這個(gè)類(lèi)成為單例類(lèi)
(2)單例類(lèi)的一個(gè)重要的特點(diǎn)就是類(lèi)的構造方法是私有的,從而避免了外部利用構造方法直接創(chuàng )造多個(gè)實(shí)例
單例類(lèi)的實(shí)現(比如設計計數器,還有Runtime這個(gè)類(lèi)):
class Singleton
{
private static final Singleton st=new Singleton();//因為為static final,所以在類(lèi)加載的時(shí)候就構造了這個(gè)實(shí)例
private Singleton(){};
public static Singleton getInstance()
{
reture st;
}