---
# System prepended metadata

title: Java_Cmoney_ft8107_ArrayList
tags: [Cmoney_Java題目]

---

---
tags: Cmoney_Java題目
---
Java_Cmoney_ft8107_ArrayList
===

![](https://i.imgur.com/wWuZOum.png)
![](https://i.imgur.com/8hXg6Tt.png)



1.需要的 function 和 class
---
1.1 class Student
---

```java=
class Stu {
    int seat;
    int score;

    Stu(int seat, int score) {
        this.seat = seat;
        this.score = score;
    }

    void print() {
        System.out.println(seat + "." + score);
    }
}
```

1.2 交換和泡沫排序
---
1. ArrayList 的長度是 .size()
2. 從ArrayList拿到特定 index .get(index)
3. 交換功能 Collections.swap (在哪個 ArrayList ,換1,換2)

```java=
public static void sortScore(ArrayList<Stu> stus) {
        for (int i = 0; i < stus.size() - 1; i++) {
            for (int j = 0; j < stus.size() - i - 1; j++) {
                if (stus.get(j).score >= stus.get(j+1).score)
                    Collections.swap(stus, j, j+1);
            }
        }
    }
```


1.3 全部印出(順序不同)
---
```java=
public static void printAlltoBig(ArrayList<Stu> stus) {
        for (int i = 0; i < stus.size(); i++) {
            stus.get(i).print();
        }
    }


public static void printAlltoSmall(ArrayList<Stu> stus) {
        for (int i = stus.size() - 1; i > -1; i--) {
            stus.get(i).print();
        }
    }
```


2.主程式
---
1. 創建方法 `ArrayList<Stu> stus = new ArrayList<Stu>();`
2. 加東西進去 `stus.add(new Stu(seat,score));`

```java=
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Stu> stus = new ArrayList<Stu>();
        int seat = 1;
        while (true) {
            int score = sc.nextInt();
            if (score == -1)
                break;
            stus.add(new Stu(seat,score));
            seat++;
        }
        sortScore(stus);
        int dir = sc.nextInt();
        if (dir == 1)
            printAlltoBig(stus);
        else
            printAlltoSmall(stus);
    }
```

3.完整程式
---
```java=
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Stu> stus = new ArrayList<Stu>();
        int seat = 1;
        while (true) {
            int score = sc.nextInt();
            if (score == -1)
                break;
            stus.add(new Stu(seat,score));
            seat++;
        }
        sortScore(stus);
        int dir = sc.nextInt();
        if (dir == 1)
            printAlltoBig(stus);
        else
            printAlltoSmall(stus);
    }

    public static void sortScore(ArrayList<Stu> stus) {
        for (int i = 0; i < stus.size() - 1; i++) {
            for (int j = 0; j < stus.size() - i - 1; j++) {
                if (stus.get(j).score >= stus.get(j+1).score)
                    Collections.swap(stus, j, j+1);
            }
        }
    }

    public static void printAlltoBig(ArrayList<Stu> stus) {
        for (int i = 0; i < stus.size(); i++) {
            stus.get(i).print();
        }
    }

    public static void printAlltoSmall(ArrayList<Stu> stus) {
        for (int i = stus.size() - 1; i > -1; i--) {
            stus.get(i).print();
        }
    }
}

class Stu {
    int seat;
    int score;

    Stu(int seat, int score) {
        this.seat = seat;
        this.score = score;
    }

    void print() {
        System.out.println(seat + "." + score);
    }
}
```