当前位置:网站首页>2022/07/14 learning notes (day07) array
2022/07/14 learning notes (day07) array
2022-07-19 06:05:00 【Radical cucumber】
Catalog :
1. demand : Find the maximum value from all elements of the array :
2. demand : Output a by “*” Composed of 5*5 The square of :
3、 ... and . Two dimensional array :
Be humble , learn , Walk forward , Look up
One . Array concept :
1.1 Container concept :
Wardrobe : All clothes are stored in one container
1.2 Array concept :
An array is a container for storing data with a fixed length , Ensure that the data types of multiple data are consistent

1.3 Definition of array :
Mode one :
Format :
The array stores the data type of the element [ ] Array name =new The data type of the data storage element [ length ]
int [ ] arr = new int [ 3 ]
Array is fixed length length [3]
Array name ——> Variable name Manipulate arrays by array names
Mode two :
Format :
The array stores the data type of the element [ ] Array name =new The data type of the data storage element [ ]{ Elements 1, Elements 2, Elements 3}
int [ ] arr = new int [ ]{10,20,30}
Mode three :
Format :
The array stores the data type of the element [ ] Array name = { Elements 1, Elements 2, Elements 3}
int [ ] arr = {10,20,30}
1.4 Array access :

Indexes : Every element stored in the array , Will automatically have a number , from 0 Start , This automatic encoding is called array indexing (index), You can index by array (index, Subscript ) Access the elements in the array
Format : Array name [ Indexes ]
String [ ] strs={" Zhang San "," Li Si "," Wang Wu "};
System.out.println(strs [ 0 ])
System.out.println(strs [ 1 ])
System.out.println(strs [ 2 ])
length : length
System.out.println(strs .length)
assignment :
strs [ 1 ] = " Zhao Liu ";
characteristic :
The length of the array cannot be changed during the running phase of the program
The data in the array , The data types must be unified
Directly print the array name to get the hash value of the address
Use advice :
1. If you are not sure about the contents of the array, it is recommended to use dynamic initialization
2. If you determine the contents of the array, it is recommended to use static initialization
1.5 Array common operations :
1.5.1 Array traversal :
//for
for ( int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
1.5.2 Array subscript out of bounds :
There is no index in the access array , The program will throw an out of bounds exception ArrayIndexOutOfBoundsException
System.out.println(strs[3]);
1.5.3 Arrays are prone to null pointer exceptions :
String[] strs={" Zhang San "," Li Si "," Wang Wu "};
strs=null;
System.out.println(strs[0]);
Exception in thread "main" java.lang.NullPointerException
Two . Array exercise :
1. demand : Find the maximum value from all elements of the array :
package Array;
public class Array02 {
public static void main(String[] args) {
int[] arr={5,15,2000,10000,4000,1500};
int max=arr[0];//0- value 》
for (int i = 0; i < arr.length; i++) {
// If you traverse to an element greater than max
if(arr[i]>max){
//max Remember the maximum value
max=arr[i];
}
}
System.out.println(" The maximum value in the array :"+max);
}
}
2. demand : Output a by “*” Composed of 5*5 The square of :
public class ch04 {
public static void main(String[] args) {
/*
* Square
* *****
* *****
* *****
* *****
* *****/
for (int i =0 ; i <5; i++) {
for (int j=0;j<5;j++){
System.out.print("*");
}
System.out.println();
}
}
}3. Clean Calculator Beta :
demand :
Enter two numbers and a symbol from the keyboard Add, subtract, multiply and divide Print out calculation results The main thing is : 1. The divisor cannot be zero 0 2. What if the symbol input is wrong ? 3. Can the accelerator keep counting ?
import java.util.Scanner;
public class jisuqi {
public static void main(String[] args) {
h2:// Start from scratch
for (; ; ) {
Scanner src = new Scanner(System.in);
System.out.println(" Please enter the first number :");
double num1 = src.nextDouble();// Enter the first number
h1:// Start with the second number
for (; ; ) {
System.out.println(" Please enter the second number :");// Enter the second number
double num2 = src.nextDouble();
System.out.println(" Please enter the operation symbol :");
String sep = src.next();
switch (sep) {
case "*":
System.out.println(num1 + sep + num2 + "=" + (num1 * num2));
break;
case "+":
System.out.println(num1 + sep + num2 + "=" + (num1 + num2));
break;
case "-":
System.out.println(num1 + sep + num2 + "=" + (num1 - num2));
break;
case "/":
if (num2 == 0) {
System.out.println(" Your input is wrong , The divisor cannot be zero 0, Please re-enter the second number :");
continue h1;// Jump to the second number
} else {
System.out.println(num1 + sep + num2 + "=" + (num1 / num2));
break ;
}
}
h3:for (;;){
System.out.println(" Do you want to continue calculating ? 1. yes 2. no ( Please enter the relevant serial number )");
int zxc=src.nextInt();// Fill in the numbers
if (zxc==1){
continue h2;// Jump to the head
}else if (zxc==2){
System.out.println(" Thank you for using !");
break h2;// Jump out of all
}else {
System.out.println(" Your input is wrong , Please re-enter ");
continue h3;// Exclude other situations
}
}
}
}
}
}4. Take a guess :
import java.util.Scanner;
public class guess {
public static void main(String[] args) {
/*
* Guess number games
* Enter a number on the keyboard and save it with a variable
* Guess what you entered
* */
/*
* Please enter a number
* 20
* Number saved successfully , Guess the number */
Scanner num = new Scanner(System.in);
System.out.println(" Please enter a number :");
int num1 = num.nextInt();
System.out.println(" Number saved successfully , Please guess the number ?");
index :for (;;) {
int num2 = num.nextInt();
if (num2 > num1) {
System.out.println(" The number you entered is larger , Please re-enter ");
continue index;
} else if (num2 < num1) {
System.out.println(" The number you entered is smaller , Please re-enter ");
continue index;
} else {
System.out.println(" congratulations , Your input is correct ");
break index;
}
}
}
}
3、 ... and . Two dimensional array :
3.1 grammar :
int [ ] arr = new int [ 3 ] [ 2 ]
1. Statement Two dimensional array name arr
2.[ 3 ] representative Three one-dimensional arrays -->3 That's ok
3.[ 2 ] representative 2 It's worth --> Column
| Column | Column | |
| That's ok | arr [ 0 ] [ 0 ] | arr [ 0 ] [ 1 ] |
| That's ok | arr [ 1 ] [ 0 ] | arr [ 1 ] [ 1 ] |
3.2 example :
demand : The company's annual sales sum
The statistical data of a company by quarter and month are as follows : Company ( Ten thousand yuan )
first quarter :22,66,44
The second quarter :77,33,88
The third quarter :25,45,65
In the fourth quarter :11,68,99
seek : Total sales :
package demo02;
public class TwoArray02 {
public static void main(String[] args) {
int[][] arrs={
{22,66,44},{77,33,88},{24,45,65},{11,66,99}};
int sum=0;
for(int i=0;i<arrs.length;i++){// Get each one-dimensional array
for(int j=0;j<arrs[i].length;j++){// Get every element of every one-dimensional array
sum+=arrs[i][j];
}
}
System.out.println(" The sales volume of this year is "+sum);
}
}


边栏推荐
- Solve cannot read properties of null (reading 'pickalgorithm')
- Wide voltage input high voltage output voltage control type
- uboot 编译前的配置命令make config分析
- 比例阀放大板1A、2A、3A、5A比例阀驱动模块0-10V转0-24V
- 模拟信号深入讨论4-20mA电流信号的传输距离
- 设置索引库结构,给用户添加可自动补全的suggestion,并将一些字段变成集合放到suggestion里面去
- 获取当前年月日、时分秒、星期,并实时更新
- Antd is not defined
- Darwin reflex summary
- 【简单快速】启动后桌面正常下方任务栏无反应/鼠标一直转圈
猜你喜欢

5-17陕西科技大学的隐藏学生服务

Rs-485/232 to 4-20ma/0-10v isolated d/a converter

4-20ma转4-20ma 0-5v转0-5v 模拟信号隔离变送器

Antd is not defined

2021-09-15

Chrome浏览器设置 【显示右上角 翻译语言图标】
![[simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning](/img/65/fbb975491d4abd5d1babdf000513e2.png)
[simple and fast] after startup, the desktop is normal, and the taskbar below is unresponsive / the mouse keeps turning

4-channel encoder pulse counter, 8-Channel do, Modbus TCP module

Hm8203 linear two string charging management controller IC

Unable to determine Electron version. Please specify an Electron version
随机推荐
RestAPI实现聚合(黑马教程)
[detailed tutorial installation] [configuration] auxiliary plug-ins about eslint in vscode
DAC7512N 模拟混合信号IC转换器
无线充发光鼠标垫RGB LED照明无线充电鼠标垫
Vscode instant English translation plug-in [translation (English Chinese Dictionary)]
busybox date 时间的加减
Speed sensor signal isolation, acquisition and transformation, sine wave and sawtooth wave signal input, square wave signal output, signal converter
c语言 指定日期开始多少天 显示
Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;
2021-09-15
MCU单片机OTP
Complete scheme diagram of lth7 five pin chip fs4054 charging circuit principle
Fs68001 wireless charging SOC chip has simple periphery and schematic diagram of 5W wireless charging scheme
[USACO06DEC]The Fewest Coins G(混合背包)
MCU单片机OTP
[transfer] Darwin streaming server core code analysis
HRA2460D-2w高压电源高压模块-高压---高精度hra2460d-2w
DSL实现自动补全查询
HRA 1~12W 系列12V《宽压9~18V》转±250VDC等升压变换器
嵌入式C语言重点(const、static、voliatile、位运算)