The latest method: JAVA array collection of hip-hop (rap) method memory flow ratio

java array

This is the way I remember very own creation of six pen:

Multi-information storage array,
eliminating variables and assignment.
It ordered set array,
the same type together.
It can be divided into one-dimensional and two-dimensional,
multi-dimensional arrays to specialized research.

How to remember:

当你想到数组时候,都有衡水三问:什么是数组,数组干什么,怎么利用数组。好的,到这里如果你能有这么三问那里也很厉害。
一串数字组合在一起,就变成了一部分数组。这时候你脑海里想到一串数字,然后有序的排列组合,接着联想到相同类型的东西组合一起也行,这里得东西指的是JAVA的数据类型。这就明白什么事数组。同理:
One-dimensional array rap notation

The method of one-dimensional array with a memory,
view data type and name.
Syntax much on two kinds of
[] one after the other with quotation marks.
A former particularly lovable,
int [] arrayName look at an example.
You may need to allocate storage room (space),
somebody new to manage.
arrayName = new array type [size (Size)];
where size is required.
The initial values are null and the system is specified,
the integer type is 0;
float type, 0.0;
character type is \ U000;
Boolean value false;
reference type is 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];

Methods and Properties : Internet have
Here Insert Picture Description

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]);
	   	 }
	 
}

Two-dimensional array of memory method:

With a two-dimensional one-dimensional,
two-dimensional sense of the same dimension.
Syntax more months [],
[] [] in front is respected.
Note that the line superscript and subscript,
get a regular element.
Use casement output,
stochastic methods have Random ();
console has a 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]);
    }
	}
}

Here is information on the console above code:

第一行第二列元素的值为: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
Published 76 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43674360/article/details/104466630