最新方法: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
发布了76 篇原创文章 · 获赞 28 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43674360/article/details/104466630
今日推荐