日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

数组(笔记九)

發(fā)布時(shí)間:2023/12/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数组(笔记九) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

  • 一、什么是數(shù)組
    • 1、數(shù)組的特點(diǎn):
    • 2、數(shù)組元素的默認(rèn)初始化值:
    • 3、演示案例
    • 4、 多維數(shù)組的使用
    • 5、一維數(shù)組和二維數(shù)組的內(nèi)存解析

一、什么是數(shù)組

數(shù)組(Array),是多個(gè)相同類(lèi)型數(shù)據(jù)按一定順序排列的集合,并使用一個(gè)名字命名,并通過(guò)編號(hào)的方式對(duì)這些數(shù)據(jù)進(jìn)行統(tǒng)一管理。

1、數(shù)組的特點(diǎn):

1)數(shù)組是有序排列的
2)數(shù)組屬于引用數(shù)據(jù)類(lèi)型的變量。數(shù)組的元素,既可以是基本數(shù)據(jù)類(lèi)型,也可以是引用數(shù)據(jù)類(lèi)型3)創(chuàng)建數(shù)組對(duì)象會(huì)在內(nèi)存中開(kāi)辟一整塊連續(xù)的空間
4)數(shù)組的長(zhǎng)度一旦確定,就不能修改。

數(shù)組的實(shí)例:

public class ArrayTest {public static void main(String[] args) {//1.一維數(shù)組的聲明和初始化int num;//聲明num =10;int id =1001;//聲明+初始化int[] ids;//聲明//1.1數(shù)組初始化:數(shù)組的初始化和數(shù)組元素的賦值操作同時(shí)進(jìn)行ids = new int[]{1001,1002,1003,1004};//1.2動(dòng)態(tài)初始化:數(shù)組的初始化和數(shù)組元素的賦值操作分開(kāi)進(jìn)行String[] names = new String[5];//錯(cuò)誤的寫(xiě)法// int[] arr1 = new int[];//×// int[5] arry2 =new int[5];//×// int[] arr3 = new int[3]{1,2,3}; //×int[] arr4 = new int[]{1,2,3}; //√//總結(jié):數(shù)組一旦初始化完成,其長(zhǎng)度就確定了。//2.如何調(diào)用數(shù)組的指定位置的元素:通過(guò)角標(biāo)的方式調(diào)用。//數(shù)組的角標(biāo)(或索引)從0開(kāi)始的,到數(shù)組的長(zhǎng)度-1結(jié)束、names[0] = "王寶軍";names[1] = "王軍";names[2] = "王鐵軍";names[3] = "王寶";names[4] = "寶軍";//3.如何獲取數(shù)組的長(zhǎng)度//屬性lengthSystem.out.println("names的長(zhǎng)度是:"+names.length);System.out.println("arr4的長(zhǎng)度是:"+arr4.length);//如何遍歷數(shù)組System.out.println("菜鳥(niǎo)級(jí)遍歷數(shù)組:");System.out.println(names[0]);System.out.println(names[2]);System.out.println(names[3]);System.out.println("for循環(huán)遍歷:");for(int i=0 ;i<names.length;i++){ //for循環(huán)遍歷System.out.println(names[i]);}System.out.println("foreach遍歷數(shù)組:");for (String name:names ) {//foreach遍歷數(shù)組System.out.println(name);}} }

2、數(shù)組元素的默認(rèn)初始化值:

public class ArrayTest1 {public static void main(String[] args) {//5.數(shù)組元素的默認(rèn)初始化System.out.println("int類(lèi)型數(shù)組的默認(rèn)值");int[] arr1 =new int[2];for (int i:arr1) {System.out.println(i);}System.out.println("String類(lèi)型數(shù)組的默認(rèn)值");String[] arr2 =new String[2];for (String i:arr2) {System.out.println(i);}System.out.println("double類(lèi)型數(shù)組的默認(rèn)值");double[] arr3 =new double[2];for (double i:arr3) {System.out.println(i);}System.out.println("char類(lèi)型數(shù)組的默認(rèn)值");char[] arr4 =new char[2];for (char i:arr4) {System.out.println(i);}System.out.println("long類(lèi)型數(shù)組的默認(rèn)值");long[] arr5 =new long[2];for (long i:arr5) {System.out.println(i);}System.out.println("float類(lèi)型數(shù)組的默認(rèn)值");float[] arr6 =new float[2];for (float i:arr6) {System.out.println(i);}System.out.println("short類(lèi)型數(shù)組的默認(rèn)值");short[] arr7 =new short[2];for (short i:arr7) {System.out.println(i);}System.out.println("boolean類(lèi)型數(shù)組的默認(rèn)值");boolean[] arr8 =new boolean[2];for (boolean i:arr8) {System.out.println(i);}} }

結(jié)果:

int類(lèi)型數(shù)組的默認(rèn)值 0 0 String類(lèi)型數(shù)組的默認(rèn)值 null null double類(lèi)型數(shù)組的默認(rèn)值 0.0 0.0 char類(lèi)型數(shù)組的默認(rèn)值long類(lèi)型數(shù)組的默認(rèn)值 0 0 float類(lèi)型數(shù)組的默認(rèn)值 0.0 0.0 short類(lèi)型數(shù)組的默認(rèn)值 0 0 boolean類(lèi)型數(shù)組的默認(rèn)值 false false

得出結(jié)論:

3、演示案例

demo1:

/*** @Description: 控制臺(tái)輸入學(xué)生人數(shù)姓名成績(jī),求出最大值以及顯示各個(gè)學(xué)生的等級(jí)$* @Author: dyq* @Date: 2021/3/17$*/ public class ArrayDemo1 {public static void main(String[] args) {//1.使用Scanner,讀取學(xué)生個(gè)數(shù)Scanner scanner = new Scanner(System.in);System.out.println("請(qǐng)輸入學(xué)生人數(shù):");int num = scanner.nextInt();//2.創(chuàng)建數(shù)組,存儲(chǔ)學(xué)生成績(jī),動(dòng)態(tài)初始化String[] name = new String[num];int[] scores = new int[num];System.out.println("請(qǐng)輸入" + num + "學(xué)生姓名:");for (int i = 0; i < scores.length; i++) {name[i] = scanner.next();}//3.給數(shù)組中的元素賦值System.out.println("請(qǐng)輸入" + num + "個(gè)學(xué)生成績(jī):");//4.獲取數(shù)組中的元素的最大值:最高分int maxScore = 0;for (int i = 0; i < scores.length; i++) {scores[i] = scanner.nextInt();if (maxScore < scores[i]) {maxScore = scores[i];}}System.out.println("學(xué)生最高分為:"+maxScore);//5.根據(jù)每個(gè)學(xué)生成績(jī)與最高分的差值,得到每個(gè)學(xué)生的等級(jí),并輸出等級(jí)和成績(jī)char level;for (int i = 0; i < scores.length; i++) {if (maxScore - scores[i] <= 10) {level = 'A';} else if (maxScore - scores[i] <= 20) {level = 'B';} else if (maxScore - scores[i] <= 30) {level = 'C';} else {level = 'D';}System.out.println("學(xué)生"+name[i]+"成績(jī)?yōu)?#34;+scores[i]+"等級(jí)為"+level);}} }

結(jié)果:

請(qǐng)輸入學(xué)生人數(shù): 4 請(qǐng)輸入4學(xué)生姓名: 張三 李四 王五 黃六 請(qǐng)輸入4個(gè)學(xué)生成績(jī): 90 98 89 79 學(xué)生最高分為:98 學(xué)生張三成績(jī)?yōu)?span id="ozvdkddzhkzd" class="token number">90等級(jí)為A 學(xué)生李四成績(jī)?yōu)?span id="ozvdkddzhkzd" class="token number">98等級(jí)為A 學(xué)生王五成績(jī)?yōu)?span id="ozvdkddzhkzd" class="token number">89等級(jí)為A 學(xué)生黃六成績(jī)?yōu)?span id="ozvdkddzhkzd" class="token number">79等級(jí)為B

**demo2:**獲取電話號(hào)碼

public class ArrayTest2 {public static void main(String[] args) {// int[] arr = new int[]{7,2,1,3,6,9,4,8};int[] arr = new int[]{7,2,1,3,6,9,4,5};int[] index = new int[]{2,0,3,0,4,3,5,2,7,6,1};String tel ="";for (int i =0;i<index.length;i++){tel+= arr[index[i]];}System.out.println("聯(lián)系方式:"+tel);} } 聯(lián)系方式:17376391542

4、 多維數(shù)組的使用

二維數(shù)組的使用
1.理解:
對(duì)于二維數(shù)組的理解,我們可以看成是一維數(shù)組array1又作為另一個(gè)一維數(shù)組array2的元素而存在。其實(shí),從數(shù)組底層的運(yùn)行機(jī)制來(lái)看,其實(shí)沒(méi)有多維數(shù)組。

二維數(shù)組初始化及其查詢遍歷:

public class ArrayTest3 {public static void main(String[] args) {//1.二維數(shù)組的聲明和初始化int[]arr = new int[]{1,2,3};//一維數(shù)組// 靜態(tài)初始化(標(biāo)準(zhǔn)寫(xiě)法)int[][]arr1 = new int[][]{{1,2,3},{4,5},{6,7,8}};//動(dòng)態(tài)初始化1String[][] arr2 = new String[3][2];//動(dòng)態(tài)初始化2String[][] arr3 = new String[3][];//錯(cuò)誤的情況/* String[][] arr4 = new String[][4];String[4][3] arr4 = new String[][];*///不標(biāo)準(zhǔn)又正確的寫(xiě)法int[] arr5[] = new int[][]{{1,2,3},{4,5},{6,7,8}};int[][] arr6= new int[][]{{1,2,3},{4,5},{6,7,8}};//2.如何調(diào)用數(shù)組的指定位置的元素System.out.println(arr1[0][1]);//2System.out.println(arr1[1][1]);//5arr3[1]=new String[4];System.out.println(arr3[1][0]);//null//3.獲取數(shù)組的長(zhǎng)度System.out.println(arr1.length);//3System.out.println(arr1[1].length);//2//4.如何遍歷二維數(shù)組System.out.println("遍歷二維數(shù)組:");for (int i =0;i<arr1.length;i++){for (int j =0;j<arr1[i].length;j++){System.out.print(arr1[i][j]+" ");}System.out.println();}} }

結(jié)果:

2 5 null 3 2 遍歷二維數(shù)組: 1 2 3 4 5 6 7 8

二維數(shù)組元素默認(rèn)初始化:

查看二維數(shù)組初始化的默認(rèn)值

public class ArrayTest4 {public static void main(String[] args) {System.out.println("--------int--------");int[][] arr = new int[4][3];System.out.println(arr[0]); //輸出地址值[I@1b6d3586 “[”表示是一維數(shù)組System.out.println(arr[0][0]);//0System.out.println(arr); //輸出[[I@4554617c “[[”表示是二維數(shù)組System.out.println("--------float--------");float[][] arr1 = new float[4][3];System.out.println(arr1[0]); //輸出地址值[I@1b6d3586 “[”表示是一維數(shù)組System.out.println(arr1[0][0]);//0.0System.out.println(arr1); //輸出[[I@4554617c “[[”表示是二維數(shù)組System.out.println("--------String--------");String[][] arr2 = new String[4][3];System.out.println(arr2[1]); //輸出地址值[Ljava.lang.String;@677327b6 “[”表示是一維數(shù)組System.out.println(arr2[1][1]);//nullSystem.out.println(arr2); //輸出[[Ljava.lang.String;@14ae5a5 “[[”表示是二維數(shù)組System.out.println("--------double--------");double[][]arr3 = new double[4][];System.out.println(arr3[1]);//nullSystem.out.println( arr3[1][0]);//報(bào)錯(cuò)|} }

輸出結(jié)果:

--------int-------- [I@1b6d3586 0 [[I@4554617c --------float-------- [F@74a14482 0.0 [[F@1540e19d --------String-------- [Ljava.lang.String;@677327b6 null [[Ljava.lang.String;@14ae5a5 --------double-------- null Exception in thread "main" java.lang.NullPointerExceptionat com.cn.ArrayTest.ArrayTest4.main(ArrayTest4.java:31)

5、一維數(shù)組和二維數(shù)組的內(nèi)存解析



擴(kuò)展練習(xí):

public class Student {int stuclass ;String name ;int number;char Grade;int age;public int getStuclass() {return stuclass;}public void setStuclass(int stuclass) {this.stuclass = stuclass;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public char getGrade() {return Grade;}public void setGrade(char grade) {Grade = grade;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [stuclass=" + stuclass + ", name=" + name + ", number=" + number + ", Grade=" + Grade + ", age="+ age + "]";}public Student(int stuclass, String name, int number, char grade, int age) {super();this.stuclass = stuclass;this.name = name;this.number = number;Grade = grade;this.age = age;}public Student() {super();} }

測(cè)試:

import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner;public class TestStudent {public static Map<String,List<Student>> map = new HashMap<String,List<Student>>();public static void saveData() {List<Student> list_401 = new ArrayList<Student>();List<Student> list_402 = new ArrayList<Student>();list_401.add(new Student(16160401,"小明", 1616040101,'A', 20));list_401.add(new Student(16160401,"小紅", 1616040102, 'B', 20));list_402.add(new Student(16160402,"校長(zhǎng)", 1616040201, 'C', 20));list_402.add(new Student(16160402,"柯南", 1616040202, 'D', 20));map.put("16160401", list_401);map.put("16160402", list_402);}public static List<Student> queryStudentByClassName(String className) {if(map.isEmpty()) {saveData();}List<Student> students = map.get(className);return students; //exit}public static void main(String[] args) {System.out.println("請(qǐng)?jiān)诳刂婆_(tái)中輸入要查找在班級(jí)名稱(chēng)");while(true) {Scanner sc = new Scanner(System.in);String className = sc.next();if("exit".equals(className)) {System.out.println("歡迎下次繼續(xù)使用");break;}List<Student> list = queryStudentByClassName(className);if(list!=null) {list.forEach((student)->{System.out.println(student.toString());});}else {System.out.println("未找到");}}} }

總結(jié)

以上是生活随笔為你收集整理的数组(笔记九)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。