最新方法:JAVA 數組集合嘻哈(rap)方法記憶流比

java 數組

這是我自己創作的記憶方法非常六筆的:

數組存儲信息多,
省去變量和賦值。
array有序的集合,
相同類型在一起。
可分一維和二維,
多維數組來專研。

如何記憶:

當你想到數組時候,都有衡水三問:什麼是數組,數組幹什麼,怎麼利用數組。好的,到這裏如果你能有這麼三問那裏也很厲害。
一串數字組合在一起,就變成了一部分數組。這時候你腦海裏想到一串數字,然後有序的排列組合,接着聯想到相同類型的東西組合一起也行,這裏得東西指的是JAVA的數據類型。這就明白什麼事數組。同理:
一維數組rap記法

一維數組記憶有方法,
來看數據類型和名稱。
語法格式不多就倆種,
[ ] 一前一後引號跟。
一前格外惹人愛,
int[ ] arrayName 看例子。
存儲可需分配房(空間),
大人物new來管理。
arrayName =new 數組類型[ size(大小)];
其中size是必須。
初始值有系統null和已指定,
整數類型值爲0;
浮點數類型爲0.0;
字符類型值爲\u000;
布爾類型值爲false;
引用類型值爲null。

相同方法 取同前者
type[] arrayName;    // 數據類型[] 數組名;

type arrayName[];    // 數據類型 數組名[];
  • eg:
// 初始化
int[] number = {1,2,3,5,8};
int[] score;    // 存儲學生的成績,類型爲整型
double[] price;    // 存儲商品的價格,類型爲浮點型
String[] name;    // 存儲商品名稱,類型爲字符串型
//分配空間
arrayName = new type[size];    // 數組名 = new 數據類型[數組長度];
score = new int[10];
price = new double[30];
name = new String[20];

方法及屬性網上都有
在這裏插入圖片描述

package array;

import java.util.Scanner;

public class ArrayTest {
	 public static void main(String[] args) {
	 //聲明數組變量
		 int[] x=null;
	   x= new int[3];
	   x[0] =10;
	   x[1] =9;
	   x[2] =8;
	    //第二種方法:
	   int[] arrayName= new int[5];
	   //獲取單個元素
	   int[] array= {1,2,3,4,5};
		System.out.println("打印第一個元素:"+array[0]);
		System.out.println("打印最後一個元素:"+array[array.length-1]);
	for(int i=0;i<array.length;i++) {
		System.out.println("依次遍歷:"+array[i]);
		
		   

	}
	//foreach方法
	for(int val:array) {
		   System.out.println("元素的值依次爲:"+val+"\t");
	   }
		
		// System.out.println("打印第六個元素:"+array[6]);//下標越界
	/*
	 * 
	 打印第一個元素:1
             打印最後一個元素:5
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
	at array.ArrayTest.main(ArrayTest.java:16)

	 */
	//問題1:編寫一個 Java 程序,使用數組存放錄入的 5 件商品價格,然後使用下標訪問第 3 個元素的值。
	  int[] prices= new int[5];
	  Scanner input= new Scanner(System.in);
	  for(int i=0;i<prices.length;i++) {
		  System.out.println("please input 第"+ (i+1)+"件商品的價格:");
	      prices[i]=input.nextInt();
	      
	  }
	   System.out.println("第三件商品的價格爲:"+prices[2]);
	   	 }
	 
}

二維數組記憶方法:

一維二維同一家,
二維道理同一維。
語法格式多加個[ ],
[ ][ ]在前面是推崇。
注意行標和下標,
獲取元素有規律。
使用鑲套在輸出,
隨機方法有random();
控制檯有scanner,system.in;

type[][] arrayName = new type[][]{1,2,3,,值 n};    // 在定義時初始化
type[][] arrayName = new type[size1][size2];    // 給定空間,在賦值
type[][] arrayName = new type[size][];    // 數組第二維長度爲空,可變化
package array;

import java.util.Arrays;
import java.util.Scanner;

public class SecendArray {

	public static void main(String[] args) {
	/*
	 * 二維數組獲取單個元素和全部元素
	 */
      double[][] CLASS_SCORE= 
    	  {{100,99,88},{100,98,97},{22,33,44},{12,13,14}};
       System.out.println("第一行第二列元素的值爲:"+CLASS_SCORE[0][1]);
       // 快速打印矩陣的方法
       System.out.println(Arrays.deepToString(CLASS_SCORE));
    for(int i=0 ;i<CLASS_SCORE.length;i++) {
    	for(int j=0;j<CLASS_SCORE[i].length;j++) {
    		System.out.println("class_score["+i+"]"+"["+j+"]"+"="+CLASS_SCORE[i][j]);
    	}
    	/*
    	 * 第一行第二列元素的值爲:99.0
class_score[0][0]=100.0
class_score[0][1]=99.0
class_score[0][2]=88.0
class_score[1][0]=100.0
class_score[1][1]=98.0
class_score[1][2]=97.0
class_score[2][0]=22.0
class_score[2][1]=33.0
class_score[2][2]=44.0
class_score[3][0]=12.0
class_score[3][1]=13.0
class_score[3][2]=14.0
    	 */
    }
        
   //問題: 假設有一個矩陣爲 5 行 5 列,該矩陣是由程序隨機產生的 10 以內數字排列而成。下面使用二維數組來創建該矩陣,代碼如下:
    // 創建一個二維矩陣  提供Math.random()方法 0-1
    int[][] matrix = new int[5][5];
    // 隨機分配值
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[i].length; j++) {
            matrix[i][j] = (int) (Math.random() * 10);
      
        }
       
    }
    System.out.println("下面是程序生成的矩陣\n");
    // 遍歷二維矩陣並輸出
    for (int k = 0; k < matrix.length; k++) {
        for (int g = 0; g < matrix[k].length; g++) {
            System.out.print(matrix[k][g] + "");
        }
        System.out.println();
	}
   //  獲取整行元素
    double[][] class_score = { { 100, 99, 99 }, { 100, 98, 97 }, { 100, 100, 99.5 }, { 99.5, 99, 98.5 } };
    Scanner scan = new Scanner(System.in);
    System.out.println("當前數組只有" + class_score.length + "行,您想查看第幾行的元素?請輸入:");
    int number = scan.nextInt();
    for (int j = 0; j < class_score[number - 1].length; j++) {
        System.out.println("第" + number + "行的第[" + j + "]個元素的值是:" + class_score[number - 1][j]);
    }
	}
}

下面是上面代碼的控制檯信息:

第一行第二列元素的值爲:99.0
[[100.0, 99.0, 88.0], [100.0, 98.0, 97.0], [22.0, 33.0, 44.0], [12.0, 13.0, 14.0]]
class_score[0][0]=100.0
class_score[0][1]=99.0
class_score[0][2]=88.0
class_score[1][0]=100.0
class_score[1][1]=98.0
class_score[1][2]=97.0
class_score[2][0]=22.0
class_score[2][1]=33.0
class_score[2][2]=44.0
class_score[3][0]=12.0
class_score[3][1]=13.0
class_score[3][2]=14.0
下面是程序生成的矩陣

29695
26868
13303
77058
13866
當前數組只有4行,您想查看第幾行的元素?請輸入:

33行的第[0]個元素的值是:100.03行的第[1]個元素的值是:100.03行的第[2]個元素的值是:99.5
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章