You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
695 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

public class Jb17_ArrDemo
{
public static void main(String[] args)
{
/*数组定义格式1
元素类型[] 数组名 = new 元素类型[元素个数或数组长度];
格式2
元素类型[] 数组名 = new 元素类型[] {元素,元素,........};
数组的操作对角标进行操作从0角标开始。
*/
//int [] arr = new int[]{2,54,4,5,4,4,5,79};
int [] arr = {-2,-5,-79,-9};
for (int x=0;x<arr.length ; x++)
{
System.out.println("arr["+x+"] = "+arr[x]);
}
int max = getMax(arr);
System.out.println("最大值是"+max);
}
//定义功能求数组最大值
public static int getMax(int[] arr)
{
int max = arr[0];
for (int x=1 ;x<arr.length ;x++ )
{
if (arr[x]>max)
{
max = arr[x];
}
}
return max;
}
}