- class MyArray {
- private int[] intList;
- public MyArray() {
- }
- public MyArray(int[] intList) {
- super();
- this.intList = intList;
- }
- public int[] getIntList() {
- return intList;
- }
- public void setIntList(int[] intList) {
- this.intList = intList;
- }
- public int getMax() {// 获取最大值
- int max = 0;
- int temp;
- for (int i = 0; i < intList.length; i++) {
- if (intList[i] > max) {
- temp = max;
- max = intList[i];
- intList[i] = temp;
- }
- }
- return max;
- }
- public int[] reverseArray() {// 反转数组的方法
- int temp;
- for (int i = 0; i < intList.length / 2; i++) {
- temp = intList[i];
- intList[i] = intList[intList.length - 1 - i];
- intList[intList.length - 1 - i] = temp;
- }
- return intList;
- }
- }
- class MyArrayTest {
- public static void main(String[] args) {
- MyArray my = new MyArray(new int[] { 1, 5, 8, 9, 4, 2, 6, 7, });
- for (int i = 0; i < my.getIntList().length; i++) {
- System.out.print(my.getIntList()[i]);
- }
- System.out.println();
- int[] newArray = my.reverseArray();
- for (int i = 0; i < my.getIntList().length; i++) {
- System.out.print(newArray[i]);
- }
- System.out.println();
- System.out.println(" max is" + my.getMax());
- }
- }
复制代码
运行结果
- 15894267
- 76249851
- max is9
- 06247851
复制代码
写的匆忙可能有没考虑全的 |