1、写出一个冒泡排序方法
public static int[] sort(int[] arr) {
int temp;
for (int i = 0; i <= arr.length; i++) {
for (int j = arr.length - 1; j > i; j--) {
if (arr[j] > arr[j - 1]) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
return arr;
}
2、写一个函数,传入一个整数型数组,抽取出其中的偶数,并输出其中的最大值
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MaxSort {
public static void main(String[] args) {
int[] arr = { 10, 11, 5, 23, 2, 6, 1 };
getMax(arr);
}
public static void getMax(int[] arr) {
List list = new ArrayList();
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
list.add(arr[i]);
}
}
Collections.sort(list); //使用Collections工具类的sort()进行排序
System.out.println("Max = " + list.get(list.size() - 1));
}
}
3、写两个线程,一个执行j++,一个执行j--
public class ThreadTest2 {
private int j;
public static void main(String[] args) {
ThreadTest2 t = new ThreadTest2();
new Thread(t.new Add()).start();
new Thread(t.new Dec()).start();
}
public synchronized void add() { //使用对象锁
j++;
System.out.println(Thread.currentThread().getName() + "-j = " + j);
}
public synchronized void dec() { //使用对象锁
j--;
System.out.println(Thread.currentThread().getName() + "-j = " + j);
}
class Add implements Runnable { //内部类Add
public void run() {
for (int i = 0; i <= 10; i++) {
add();
}
}
}
class Dec implements Runnable { //内部类Dec
public void run() {
for (int i = 0; i <= 10; i++) {
dec();
}
}
}
}
相关文章: