内容简介:Java基础知识:数组
一、大数据
如果基本的整型和浮点型精度不能够满足需求,那么可以使用java.math包含中的两个类:BigInteger和BigDecimal。
这两个类处理包含任意长度数字序列的数值。BigInteger类实现了任意精度的整型运算,BigDecimal实现了任意精度的浮点数运算。
使用静态的valueOf方法可以讲欧通的数值转换为大数据:
BigInteger a=BigInteger.valueOf(700);
还有,人们不能用加(+)或减(-)等来处理大数值。而必须用类中的add和multiply方法。
BigInteger c=a.add(b);//c=a+b
BigInteger d=c.multiply(b.add(BigInteger.valueOf(2)));//d=c*(b+a)
二、数组
数组是一种数据结构,用来储存同一类型值的集合
1.声明了一个整型数组a: int [] a;
2.创建数组a: int [] a=new int[100];
1 import java.util.Arrays; 2 public class Test2 { 3 public static void main(String[]args){ 4 int [] a=new int[]{5,-1,-88,2,4,44,12,-90}; 5 Arrays.sort(a); 6 for(int i=0;i<a.length;i++){ 7 System.out.println(a[i]); 8 } 9 } 10 }
1 public class Test1 { 2 public static void main(String[]args){ 3 int [] a={5,-1,-88,2,4,44,12,-90};//用冒泡的方法把它由小到大的排序 4 sort(a); 5 for(int i=0;i<a.length;i++){ 6 System.out.println(a[i]); 7 }//函数执行完,再数一遍 8 } 9 static void sort(int[]a){ 10 for(int i=0;i<a.length-1;i++){ 11 for(int j=0;j<a.length-1-i;j++) 12 if(a[j]>a[j+1]) 13 { 14 int temp=a[j]; 15 a[j]=a[j+1]; 16 a[j+1]=temp; 17 } 18 } 19 } 20 } 21 /*如果上面i<a.length-1和j<a.length-1-i都填上等号变成 i<=a.length-1和j<=a.length-1-i 22 * 则会发生脚标越界异常的情况,如下: 23 * Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 24 */
注意:
* 创建一个数组时,所有元素都初始化为0;
* boolean数组的元素会初始化为false;
* 对象数组的元素则初始化为一个特殊值null,这表示这些元素还未存放任何对象。(比如:for(int i=0;i<100;i++)names[i]="";
三、数组的拷贝
要使用Arrays类的copyOf方法:
int [] a=Arrays.copyOf(a,a.length);
四、多维数组
多维数组将使用多个下标访问数组元素,它适用于表示表格或更复杂的排列形式。
格式类似如下:
int [] [] a={{.......},{.......},{.......},{.......},{.......}}
提示:如果想要打印一个二维数组的数据元素列表,可以调用:
System.out.println(Arrays.deepToString(a));
输出格式为:
[[1,2,3,3],[4,5,7,65],[7,5,7,8]]
例子:
1 /*This program show how to store tabular data in a 2D array. 2 * @version 1.40 2004-02-10 3 * @author Cay Horstmann 4 */ 5 public class CompoundInterest { 6 public static void main(String[] args) { 7 final double STARTRATE = 10; 8 final int NRATES = 12; 9 final int NYEARS = 10; 10 11 double[] interestRete = new double[NRATES]; 12 for (int j = 0; j < interestRete.length; j++) 13 interestRete[j] = (STARTRATE + j) / 100.0; 14 15 double[][] balances = new double[NYEARS][NRATES]; 16 for (int j = 0; j < balances[0].length; j++) 17 balances[0][j] = 120000; 18 19 for (int j = 1; j < balances.length; j++) { 20 for (int j1 = 0; j1 < balances[j].length; j1++) 21 { 22 double oldBalance = balances[j-1][j1]; 23 24 double interest = oldBalance * interestRete[j1]; 25 balances[j][j1] = oldBalance + interest; 26 } 27 } 28 for (int j = 0; j < interestRete.length; j++) 29 System.out.printf("%9.0f%%",100*interestRete[j]); 30 System.out.println(); 31 for (double[] row : balances) { 32 for (double b : row) 33 System.out.printf("%10.2f", b); 34 System.out.println(); 35 } 36 } 37 }
打印结果:
五:不规则数组
例子:
1 /** 2 * This program demonstrates a triangular array. 3 * 4 * @version 1.20 2004-02-10 5 * @author Cay Horstmann 6 */ 7 public class LotterArray { 8 public static void main(String[] args) { 9 final int NMAX = 10; 10 11 int[][] odds = new int[NMAX + 1][]; 12 for (int n = 0; n <= NMAX; n++) 13 odds[n] = new int[n + 1]; 14 15 for (int n = 0; n < odds.length; n++) 16 for (int k = 0; k < odds[n].length; k++) { 17 int lotteryOdds = 1; 18 for (int i = 1; i <= k; i++) 19 lotteryOdds = lotteryOdds * (n - i + 1) / i; 20 odds[n][k] = lotteryOdds; 21 } 22 for (int[] row : odds) { 23 for (int odd : row) 24 System.out.printf("%4d", odd); 25 System.out.println(); 26 } 27 } 28 29 }
打印结果:
本文永久更新链接地址 : http://www.linuxidc.com/Linux/2017-06/144877.htm
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python Algorithms
Magnus Lie Hetland / Apress / 2010-11-24 / USD 49.99
Python Algorithms explains the Python approach to algorithm analysis and design. Written by Magnus Lie Hetland, author of Beginning Python, this book is sharply focused on classical algorithms, but it......一起来看看 《Python Algorithms》 这本书的介绍吧!