🎶 Sym - 一款用 Java 实现的现代化社区(论坛/BBS/社交网络/博客)平台

📕 思源笔记 - 一款桌面端笔记应用,支持 Windows、Mac 和 Linux

🎸 Solo - B3log 分布式社区的博客端节点,欢迎加入下一代社区网络

♏ Vditor - 一款浏览器端的 Markdown 编辑器

Java 基本的程序设计结构(五)

(九)大数值
这个东东一般不太常用,所以在此就简单说下吧
首先,超过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());


欢迎注册黑客派社区,开启你的博客之旅。让学习和分享成为一种习惯!

留下你的脚步