int[] nums;
nums = new int[10];
//第二种: int[] nums = new int[10]
数组的创建
Scanner input1=new Scanner(System.in);
for (int i=0;i<10;i++){
nums[i]=input1.nextInt();
}
for (int i=0;i<10;i++){
System.out.print(nums[i]+" ");
}
int[] arrays={1,2,3,5,4,98};
数组的各种操作
输出打印
public static void print(int array[]){
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
反向操作
public static void retrun(){
Scanner input1=new Scanner(System.in);
int[] num1=new int[5];
int[] num2=new int[5];
for (int i = 0; i < num1.length; i++) {
num1[i]=input1.nextInt();
}
for (int i=0, j=num2.length-1;i<num1.length;i++,j--){
num2[j]=num1[i];
}
print(num2);
}
比较大小
public static void compare(
){ Scanner input1=new Scanner(System.in);
System.out.println("请输入比较几个数");
int a=input1.nextInt();
int[] array=new int[a];
for (int i = 0; i < a; i++) {
array[i]=input1.nextInt();
}
int max=array[0];
for (int i = 1; i < array.length; i++) {
if(array[i]>max){
max=array[i];
}
System.out.println(max);
}
完整代码
package base.base.base;
import java.util.Scanner;
public class ArryDemo01 {
public static void main(String[] args) {
/*//两种数组的创建方法
int[] nums;
nums = new int[10];
//第二种: int[] nums = new int[10]
下面是数组的操作
/*
数组的反转
*/
//retrun();
compare();
}
public static void retrun(){
Scanner input1=new Scanner(System.in);
int[] num1=new int[5];
int[] num2=new int[5];
for (int i = 0; i < num1.length; i++) {
num1[i]=input1.nextInt();
}
for (int i=0, j=num2.length-1;i<num1.length;i++,j--){
num2[j]=num1[i];
}
print(num2);
}
public static void print(int array[]){
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
//比较大小的
public static void compare(
){ Scanner input1=new Scanner(System.in);
System.out.println("请输入比较几个数");
int a=input1.nextInt();
int[] array=new int[a];
for (int i = 0; i < a; i++) {
array[i]=input1.nextInt();
}
int max=array[0];
for (int i = 1; i < array.length; i++) {
if(array[i]>max){
max=array[i];
}
System.out.println(max);
}
}
}