Array traversal

packageJava base 04_ one-dimensional array;/* * Note: arrays provide a property. length is used to get the length of the array */
public class ArrayDemo3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] str = {"2"."3"."4"."5"."6"."Seven"."8"."9"."22"."52"."72"};
		// Need: walk through the array of STR, and identify the last element of the array, tell me which is the last element.
		System.out.println(str.length);
		for(int i =0; i<str.length; i++) {if(i==str.length-1) {
				System.out.println("The last element is:"+str[i]);
			}else {
				System.out.println(str[i+1]); }}}}Copy the code
packageJava base 04_ one-dimensional array;Analysis: * 1: define an array and statically initialize its elements * 2: Select any element from the array as a reference, usually the first element, pretend that the first element is the maximum, * 3: Then walk through the other elements, get and compare with the reference in turn, if the big is the next reference, if the small is gone, * 4: the last reference is the maximum saved */
public class ArrayDemo4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stubq
		int[] arr= {32.52.6.42};
		int max = arr[0];/ / 32
		int little=arr[0];
		for(int i =0; i<arr.length-1; i++) {if(arr[i]>max) {//
				max=arr[i];
			}
		}
		System.out.println("Maximum value:"+max);
		
		/* * find the minimum value: */
		for(int i =0; i<arr.length-1; i++) {if(arr[i]<little) {//
				little=arr[i];
			}
		}
		System.out.println("Minimum:"+little); }}Copy the code