Java基本的程式設計結構(五)

來源:互聯網
上載者:User
(九)大數值
這個東東一般不太常用,所以在此就簡單說下吧
首先,超過32位就要使用它,是包含在java.math包中的兩個很有用的類
BigInteger和BigDecimal

需要注意的是,他們不能使用-+/*=,需要使用add(); subtract();
multiply();
divide()
mod();
compareTo();
valueOf();

(十)數組

初始化
int a[] = new int[20];
int c[];  // declare array variable
 c = new int[ 12 ]; // create array
for (int i=0; i<a.length; i++)
    a[i] = i;
for(int i : a)
    System.out.println(i);

You can declare an array variable either as
int[] a; or as
int a[];

 1."for each"迴圈

for (int element : a)
     System.out.println(element);
上面那個等同與下面這個
for (int i = 0; i < a.length; i++)
     System.out.println(a[i]);

2.數組初始化器和匿名數組
int smallInts[] = {1,2,3};
String[] cardinals = {"one","two","three"};
String[] ordinals = {"first","second","third"};

匿名數組
new int[]{12, 23, 23, 23}
一般好像沒什麼用處阿

3. 數組拷貝
System.arraycopy(from, fromIndex, to, toIndex, count);
例子:
int[] smallPrimes = {2, 3, 5, 7, 11, 13};
int[] luckyNumbers = {1001, 1002, 1003, 1004, 1005, 1006, 1007};
System.arraycopy(smallPrimes, 2, luckyNumbers, 3, 4);
for (int i = 0; i < luckyNumbers.length; i++)
   System.out.println(i + ": " + luckyNumbers[i]);

結果:
0:1001
1:1002
2:1003
3:5
4:7
5:11
6:13

4.命令列參數
 public static void main(String[] args) {

        for (int i=0; i<args.length; i++)
            System.out.println(args[i]);
   
}

5.數組排序
(Math.random() * n;  //產生隨機數  0到n-1
random()產生0-1(不含1的浮點數)

 Arrays.sort(result); //排序

6.多維陣列

balance = new double[NYEARS][NRATES]; 
// you cannot use the array until you new and initialize it

int[][] magicSquare =
   {
      {16, 3, 2, 13, 23},
      {5, 10, 11, 8, 3},
      {9, 6, 7, 12, 67},
      {4, 15, 14, 1, 44}
   };             // note: no “new”

7.不規則數組
也是就每行長度不一樣
初始化的例子:
int [][] odds = new int [NMA+1][];
for(int i = 0; i <= NMA; n++)
{
    odds[n] =new int[n+1];
}
for(int n = 0; n < odds.length(); n++)
{
    for(int k = 0; k < odds[n].length(); k++)
    {
         odds[n][k] = lotteryOdds; 
    }
}

8.vector
類似於數組,但是不需要聲明大小
可以包含不同的物件類型

初始化:
Vector vec = new Vector();

有用的一些方法:
isEmpty()
indexOf(Object arg)
contains(Object arg)

例子:
Vector vec = new Vector();
vec.addElement(“one”);
vec.addElement(“two”);
vec.addElement(“three”);
Enumeration e = vec.elements();
while (e.hasMoreElements()) {
      System.out.println(“element = “ + e.nextElement());
}
結果:
element = one
element = two
element = three

9.Collection
constructor()
Creates a collection with no elements
size()
Number of elements in the collection
boolean add()
Add a new pointer/element at the end of the collection
Returns true is the collection is modified.
iterator()
Returns an Iterator Object
hasNext() - Returns true if more elements
next() - Returns the next element
remove() - removes element returned by previous next() call.

例子
    it = strings.iterator();
    while (it.hasNext()) {
        it.next();        // get pointer to elem
        it.remove();    // remove the above elem
    }
   
    System.out.println("size:" + strings.size());

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.