本文目錄一覽:
- 1、Java定義一個一維數組有哪幾種方法
- 2、java中一維數組的定義及初始化的方法有哪幾種?
- 3、java 判定一個數組是否是一維數組
- 4、JAVA用一維數組做一個示例。
- 5、用java一維數組的遍歷:輸出數組中的所有元素值,要求打印格式為: [元素值1,元?
- 6、Java一維數組的增刪改查 急!!
Java定義一個一維數組有哪幾種方法
首先: 一維數組的聲明 ; 代碼如下
int[] ary1; //Java推薦的數組聲明方式
int ary2[]; // 不推薦,但也是正確的,C語言等使用這種方式
其次: 聲明的聲明和賦值
//參考一
int[] ary1 = {1,2,3};// 聲明的時候,同時賦值
//參考二
int[] ary2 = new int[3];//指定數組的長度為3
ary2[0] = 1; //數組下標從0開始, 依次給每個元素賦值
ary2[1] = 2;
ary2[2] = 3;
//參考三
int[] ary3 = new int[] {1,2,3}; //注意, 這裡的new int[]方框里不能指定長度
最後: 注意數組的默認值;
簡單數據類型,比如boolean類型的數組,默認值就是false, int類型的數組,默認值就是0.
對象類型的數組,比如Boolean類型的數組,默認值是null,Integer類型的數組,默認值還是null
int[] ary1 = new int[1];
System.out.println(ary1[0]); //輸出0
Integer[] ary2=new Integer[1];
System.out.println(ary2[0]); //輸出null
boolean[] b1=new boolean[1];
System.out.println(b1[0]); //輸出false
Boolean[] b2=new Boolean[1];
System.out.println(b2[0]); //輸出null
java中一維數組的定義及初始化的方法有哪幾種?
兩種:
1 int a[]=new int[10]
for(int i=0;i10’i++){
a[i]=i+1;
}
2 int[] b={1,2,3,4,5,6}
java 判定一個數組是否是一維數組
一維數組的定義
//定義包含三個元素的一維數組
int[] a = new int[3]; a = {1,2,3};//方法1,先new對象,然後賦值
int[] b = {1,2,3}; //方法2,直接賦值
int[] c = new int[]{1,2,3}; //方法3,new後直接賦值,注意不能制定長度
javascript中: String s = new Array(“a”,”b”); 或者 String s = [“a”,”b”]; 或者 String s = new Array(); s.push(“a”);s.push(“b”);
注意:如果用new定義數組時,必須指定其維度,這樣定義是錯誤的: int[] d = new int[];
如果無法確定其元素個數,可以這樣定義:int[] e = {};
這樣也是錯誤的: int[] c = new int[3]{1,2,3}; 因為初始化時候已經賦值為0;只能為
int[] c = new int[]{1,2,3};
JAVA用一維數組做一個示例。
//一維數組
String[] str = new String[5]; //創建一個長度為5的String(字符串)型的一維數組
str[0] = “a”; //賦值
str[1] = “b”;
str[2] = “c”;
str[3] = “d”;
str[4] = “e”;
//也可以創建並賦值
String[] str = {“a”,”b”,”c”,”d”,”e”};
//二維數組
String[][] str = new String[2][2]; //創建一個2行2列的二維數組
str[0][0] = “a0”; //賦值
str[0][1] = “a1”;
str[1][0] = “b0”;
str[1][1] = “b1”;
//也可以創建並賦值
String[][] str = {{“a0″,”a1”},{“b0″,”b1”}};
總結:
類型名[]/[][] 變量名 = new 類型名[]/[][];
用java一維數組的遍歷:輸出數組中的所有元素值,要求打印格式為: [元素值1,元?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class DemoTest05 {
public static void main(String[] args) {
/*
*確定數組的長度
*/
Scanner sc = new Scanner(System.in);
System.out.println(“請輸入數組的大小:”);
int num = sc.nextInt();
int[] arr1 = new int[num];
//逐個輸入
System.out.println(“請逐個輸入數組元素,按回車鍵確定並繼續…”);
for (int i = 0; i arr1.length; i++) {
arr1[i] = sc.nextInt();
}
//數組沒有重寫toString方法,會直接打印數組的地址,使用Arrays工具類的toString方法輸出
System.out.println(Arrays.toString(arr1));
sc.nextLine();//過濾緩衝區中的回車
/*
不確定數組的長度,使用ArrayList集合
*/
ListInteger list = new ArrayListInteger();
System.out.println(“請輸入數組元素,元素間使用空格分隔,按回車鍵結束…”);
String[] strings = sc.nextLine().split(” “);
for (int i = 0; i strings.length; i++) {
list.add(Integer.parseInt(strings[i]));
}
System.out.println(list);
}
}
Java一維數組的增刪改查 急!!
代碼如下:
public class Main {
public static void main(String[] args) {
int[] a = new int[]{92, 87, 2, 3, 4, 6, 7, 8, 22, 9, 12, 16, 20, 66, 23};
findNum(a, 6);
findNum(a, 300);
}
private static void findNum(int[] a, int num) {
for (int i = 0; i a.length; i++) {
if (a[i] == num) {
System.out.println(“在數組中找到了” + num + “,位於數組的” + i + “位置”);
return;
}
}
System.out.println(“數組中沒有” + num + “這個數字”);
}
}
運行結果:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/254060.html