---
# System prepended metadata

title: CS61B HW0 - Array elements 比大小
tags: [Practice, Java]

---

# CS61B HW0 - Array elements 比大小

###### tags: `Java` `Practice`

```
package cs61b;

public class ArrayMax {
	// return the max value from the array.
	
	public static int max(int[] m) {
		
		int MAX = 0;
		for (int i = 0; i < m.length; i++) {
			if (m[i] > MAX) {
				MAX = m[i];
			}
			else {
				
			}
		}
		return MAX;
	}
	
	

	public static void main(String[] args) {
		int[] numbers = new int[] {9, 2, 15, 2, 22, 10, 6};
		
		System.out.println(max(numbers));

	}

}

```

1. 設定一個假想的最大值為MAX，初始值為0，並利用for loop traverse整個陣列逐一與MAX比較，若比它大則取代為新的MAX值
2. 最後呼叫該method並print結果出來即可