java的常用引用类、数组、String类
? ? ? ? ? ? ? ? ? ? ? ? ?java的常用引用類、數組、String類
1. 常用引用類
1.1 Scanner?
? ? ? ? ? ? ? ? ? ? ??一個簡單的文本掃描器類。
? ? ? ? ? ? ? ? ? ? ??使用:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//創建掃描器對象
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Scanner sc = new Scanner(System.in);?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//接收用戶輸入字符串類型的數據?? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String str = sc.next();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//接收用戶輸入整數類型的數據??? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?int num = sc.nextInt(); ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//接收用戶輸入小數類型的數據?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?double d = sc.nextDouble();
Scanner sc = new Scanner(System.in); System.out.print("請輸入姓名:"); //輸入姓名 String name = sc.next(); //輸入年齡 System.out.println("請輸入年齡 :"); int age = sc.nextInt(); //輸入工資 System.out.print("請輸入工資:"); double salary = sc.nextDouble(); //輸出 System.out.println("姓名:"+name+",年齡:"+age+",工資:"+salary);1.2?Random
? ? ? ? ? ? ? ? ? ? ??該類的實例用于生成偽隨機數的流。
使用:
? ? ? ? ? ?//創建Random對象? ? ? ? ? ?
? ? ? ? ? ?Random rand = new Random();
? ? ? ? ? ?//0~9 任意一個整數 ?[0,10)
? ? ? ? ? ?int n = rand.nextInt(10);
? ? ? ? ? ??//0.0-1.0任意一個double數[0.0,1.0)
? ? ? ? ? ?double d =rand.nextDouble();
Random rand = new Random(); //生成0~9 任意一個 int n = rand.nextInt(10); //生成0.0~1.0 任意一個double數 double d =rand.nextDouble(); System.out.println(n); System.out.println(d);1.3?Math
? ? ? ? ? ? ? ? ? ? ??Math類包含執行基本數字運算的方法,如基本指數,對數,平方根和三角函數。
? ? ? ? ? ? ? ? ? ? ??常用值與函數:
| 函數 | 描述 |
| Math.random | 返回0,1之間的一個隨機數 |
| Math.max | 求兩數中最大? |
| Math.min | 求兩數中最小? |
| Math.sqrt | 求開方? |
| Math.ceil | 得到不小于某數的最大整數? |
| Math.floor | 得到不大于某數的最大整數? |
| Math.rint ? | 求距離某數最近的整數(可能比某數大,也可能比它小) |
| Math.round | 同上,返回int型或者long型(上一個函數返回double型)? |
| Math.exp | 求e的任意次方? |
| Math.log10 | 以10為底的對數? |
| Math.log | 自然對數 |
| Math.pow | 求某數的任意次方, 拋出ArithmeticException處理溢出異常 |
| Math.PI | 記錄的圓周率 |
| Math.E | 記錄e的常量? |
| Math.abs | 求絕對值? |
| Math.sin | 正弦函數 |
| Math.asin | 反正弦函數? |
| Math.cos | 余弦函數 |
| Math.tan | 正切函數 |
| Math.toDegrees | 弧度轉化為角度 |
| Math.toRadians | 角度轉化為弧度? |
| Math.IEEEremainder | 求余? |
? ? ? ? ? ? ? ? ? ? ? 使用:? //double類型的數據,[0.0,1.0)
? ? ? ? ? ?Math.random();?
double d = Math.random(); //生成0-1的整數 double random = Math.random(); System.out.println(random); //生成1-10的整數 int n = (int)(Math.random()*10)+1; System.out.println(n);?
2.0 數組
數組概念:可以存儲一組相同類型的數據。
2.1 一維數組
? ? ? ? ? ? ? ? ? ? ? 語法:(1)類型[] ?數組名;?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(2)類型 ?數組名[];
? ? ? ? ? ? ? ? ? ? ? 靜態初始化:(1)類型[] 數組名?= new 類型[]{值1,值2,…};
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (2)類型[] 數組名?;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?數組名?= {值1,值2,…};
? ? ? ? ? ? ? ? ? ? ? 動態初始化:類型[] 數組名?= new 類型[長度];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?必須要添加長度,一旦分配長度,該數組的空間是不可改變的;賦值:可以通過下標給數組賦值;元? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 素的個數:length可以獲取數組中元素的個數
? ? ? ? ? ? ? ? ? ? ? 注意事項:(1)數組中下標是從0開始
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (2)空指針異常:java.lang.NullPointerException
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(3)索引越界異常:java.lang.ArrayIndexOutOfBoundsException
? ? ? ? ? ? ? ? ? ? ?數組的操作:(1)求最大值? ? ?
int array[] = new int[] {11,55,22,99,88}; //最大值 int max = array[0]; //遍歷數組 for(int i=1;i<array.length;i++) {if(max<array[i]) {max=array[i];} } System.out.println("最大值 :"+max);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(2)最小值
int array[] = new int[] {11,55,22,99,88}; //最小值 int min = array[0]; //遍歷數組 for(int i=1;i<array.length;i++) {if(min > array[i]) {min = array[i];} } System.out.println("最小值 :"+ min );? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(3)求和
int array[] = new int[] {11,55,22,99,88}; //數組中的和 int sum = 0; //遍歷數組 for(int i=0;i<array.length;i++) {sum += array[i]; } System.out.println("數組的和 :"+ sum );? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(4)平均值
int array[] = new int[] {11,55,22,99,88}; //數組中的和 double sum = 0; //遍歷數組 for(int i=0;i<array.length;i++) {sum += array[i]; } //平均值 double avg = sum/array.length; System.out.println("數組的平均值 :"+ avg );? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(5)復制
//舊數組 int[] array = new int[]{11.55.88.33.66.22} //新數組 int[] newArray = new int[array.length]; //遍歷舊數組 for(int i=0;i<array.length;i++) {newArray[i]=array [i]; }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(6)反轉
int array[] = new int[] {11,55,22,99,88}; System.out.println("反轉前:"); for(int i=0;i<array.length;i++) {System.out.print(array[i]+" "); } //遍歷數組 for(int i=0;i<array.length/2;i++) {//交換位置array[array.length -1 -i] = array[i]^array[array.length -1 -i];array[i] = array[array.length -1 -i]^array[i];array[array.length -1 -i] = array[array.length -1 -i]^array[i]; } System.out.println("反轉后:"); for(int i=0;i<array.length;i++) {System.out.print(array[i]+" "); }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(7)冒泡排序
int array[] = new int[] {11,55,22,99,88}; //遍歷數組 for(int i=0;i<array.length -1;i++) {for(int j = 0;j < array.length - 1;j++) {if(array[j] > array[j+1]){array[j+1] = array[j+1]^array[j];array[j] = array[j+1]^array[j];array[j+1] = array[j+1]^array[j];}} } System.out.print("按從小到大順序:" ); for(int i=0;i<array.length;i++) {System.out.print(array[i]+" "); }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(8)選擇排序
int array[] = new int[] {11,55,22,99,88}; //遍歷數組 for(int i=0;i<array.length -1;i++) {int z = i;for(int j = i+1;j < array.length;j++) {if(array[z] > array[j]){z = j;}}if(z != i) {array[z] = array[z]^array[i];array[i] = array[z]^array[i];array[i] = array[z]^array[i];} } System.out.print("按從小到大順序:" ); for(int i=0;i<array.length;i++) {System.out.print(array[i]+" "); }2.2 二維數組
? ? ? ? ? ? ? ? ? ? ??語法:(1)類型[][]? 數組名;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(2)類型? 數組名[][];
? ? ? ? ? ? ? ? ? ? ? 靜態初始化:(1)類型[][]? 數組名?= new 類型[][]{{值1,值2},{值3,值4}…};
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (2)類型[][] 數組名?;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?數組名?= {{值1,值2},{值3,值4}…};
? ? ? ? ? ? ? ? ? ? ? 動態初始化:類型[][] 數組名?= new 類型[長度1][長度2];
? ? ? ? ? ? ? ? ? ? ? 注意:二維數組可以看作行或列,由于數組的下標起始值為 0,因此行和列的下標需要減 1。
? ? ? ? ? ? ? ? ? ? ? 二維數組的操作:(1)獲取單個元素
int[][] array = {{1,2},{3,4,5},{6,7,8,9}}; //打印數組的行數 System.out.println(arr.length); //打印數組第2行的擁有的元素即列數 System.out.println(arr[1]);//打印數組第1行第2列的元素 System.out.println(arr[0][1]);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(2)獲取整行元素
int[][] array = {{1,2},{3,4,5},{6,7,8,9}}; Scanner scan=new Scanner(System.in); System.out.println("當前數組有"+array.length+"行,請輸入您要查看的行數:"); int num =scan.nextInt(); for(int i=0;j<array[num-1].length;i++) {System.out.println("第"+num+"行的第["+i+"]個元素的值是:"+array[num-1][i]); }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(3)獲取整列元素
int[][] array = {{1,2},{3,4,5},{6,7,8,9}}; Scanner scan=new Scanner(System.in); System.out.println("請輸入您要獲取的列:"); int num=scan.nextInt(); for(int i=0;i<array.length;i++) {System.out.println("第 "+(i+1)+" 行的第["+num+"]個元素的值是"+array[i][num]); }? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?(4)遍歷
int[][] array = {{1,2},{3,4,5},{6,7,8,9}}; //遍歷二維數組 for(int i=0;i<arr.length;i++) {for(int j=0;j<arr[i].length;j++) {System.out.print(arr[i][j]+" ");} }?
3 String
String類代表字符串,?字符串不變,它們的值在創建后不能被更改
StringBuffer 和 StringBuilder 類的對象能夠被多次的修改,并且不產生新的未使用對象。tringBuilder 相較于 StringBuffer 有速度優勢,所以多數情況下建議使用 StringBuilder 類。然而在應用程序要求線程安全的情況下,則必須使用 StringBuffer 類。
String s1 = "abc"; s1+="bcd"; //重新開辟空間 //synchronized StringBuffer buf =new StringBuffer("abc"); buf.append("bcd"); //效率高 StringBuilder str = new StringBuilder("abc"); str.append("bcd"); //不會開辟新空間 System.out.println(s1); //abcbcd System.out.println(str.toString()); //StringBuilder>StringBuffer>String 效率高到底字符串在內存中是以字符的方式存儲的。
String str = "abc";相當于: char data[] = {'a', 'b', 'c'};String str = new String(data);
字符串的比較見如下代碼:
public static void main(String[] args) {String s1 = "abc";String s2 = "abc";String s3 = new String("abc");//== 地址//equals比較字符串,比較每個字符System.out.println(s1==s2); //trueSystem.out.println(s1==s3); //falseSystem.out.println(s1.equals(s3)); //trueString s4 ="ab"+"c"; //編譯時確定String s5 ="ab"+ new String("c"); //運行時確定String s6 ="ab";String s7 = "c";String s8 =s6+s7;System.out.println("=======================");System.out.println("s4==s1:"+(s4==s1)); //trueSystem.out.println("s5==s1:"+(s5==s1)); //falseSystem.out.println("s8==s1:"+(s8==s1)); //false//String s9 = s8.intern(); //在常量池地址System.out.println("s9==s1:"+(s8.intern()==s1)); //trueSystem.out.println(s8.equals(s5)); //true }?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的java的常用引用类、数组、String类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ae中心点重置工具_7步学习AE 入门篇
- 下一篇: Spark Worker源码