大數之間的加減乘除運算不能直接用+-*/來操作,Java中也未提供運算符重載,具體加減乘除的方法參考JDK文檔
二、數組
Java中匿名數組:
例:new int[]{1,2,3,4,5} 匿名數組一般用於改數組只用於一次的場景中,即用了之後以後基本就不用了。
Java中可以創建一個長度爲0的數組。例:int[] s = new int[0]
數組長度爲0與爲NULL不同。
數組拷貝:
例如: int[] temparray1= new int[]{1,2,3,4,5};
int[] temparray2 = temparray1;
上述代碼中的拷貝只是數組temparray1與temparray2共同指向同一個數組
System.arraycopy(from,fromindex,to,toindex,count)
實現從數組from中第fromindex後的count個元素拷貝到to數組的第toindex之後。
數組排序:Arrays.sort(); 內部使用了優化的快速排序方法。
多維數組:一般二維數組,具體見程序
import java.util.* ;
public class Test9
{
public static void main(String[] args)
{
double[] arrayRate = new double[]{0.35,2.60,2.80,3.00,3.75}; //存款利率
double capital = 100000.00 ; //本金
int MYear=6; //年限
double[][] Money = new double[MYear][arrayRate.length];
for(int i=0;i<MYear;i++)
{
for(int j=0;j<arrayRate.length;j++)
{
if(i==0)
{
Money[i][j] = capital*arrayRate[j]/100.00+capital;
}
else
{
Money[i][j] = Money[i-1][j]*arrayRate[j]/100.00+Money[i-1][j];
}
}
}
for(double[] tempArray:Money)
{
for(double tempMoney:tempArray)
{
System.out.printf("%10.2f ",tempMoney);
}
System.out.println("");
System.out.println("======================================================");
}
}
}
輸出結果:
不規則數組:
對於不規則數組,只能先聲明其行數,然後再對每一行聲明其列數,例如:
int[][] TempArray = new int[5][];
for(int i=0;i<5;i++)
{
TempArray[i] = new int[i+1];
}
三、命令行參數
Java程序中都有一個帶有String[] args 參數的main方法,表明main方法接收一個字符串數組,即命令行參數。
public class Test7
{
public static void main(String[] args)
{
if(args.length==0)
{
System.out.println("Sorry! No Parameters.");
}
else
{
if(args[0].equals("-h"))
{
System.out.print("Hello! ");
}
if(args[0].equals("-g"))
{
System.out.print("GoodBye! ");
}
for(int i=1;i<args.length;i++)
{
System.out.print(args[i]);
}
System.out.println("!");
}
}
}
輸出結果: