請問該如何修改?
謝謝幫助!
建個包整個拷貝貼上就可以跑了
有不懂歡迎發問
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
public class itTest {
public static void main(String[] args) {
int[] data = new int[10];
Random ran = new Random();
for (int i = 0; i < data.length; i++) {
data[i] = ran.nextInt(100) + 1;
}
BigData bigData = new BigData(data);
System.out.println("before sort:" + bigData.toString());
System.out.println(bigData.search(30));
bigData.sort();
System.out.println("after sort:" + bigData.toString());
/**
before sort:67,50,2,18,19,86,88,6,47,51
false
after sort:88,86,67,51,50,47,19,18,6,2
*/
}
}
class BigData {
private int[] number;
public BigData(int[] sample) {
this.number = sample;
}
public boolean search(int data) {
return Arrays.binarySearch(this.number, data) >= 0;
}
public void sort() {
Arrays.sort(this.number);
for (int i = 0; i < number.length / 2; i++) {
int temp = number[i];
number[i] = number[number.length - i - 1];
number[number.length - i - 1] = temp;
}
}
public String toString() {
return Arrays.stream(number).mapToObj(String::valueOf).collect(Collectors.joining(","));
}
}
package demo;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
public class itTest {
public static void main(String[] args) {
int[] data = new int[10];
Random ran = new Random();
for (int i = 0; i < data.length; i++) {
data[i] = ran.nextInt(100) + 1;
}
BigData bigData = new BigData(data);
System.out.println("before sort:" + bigData.toString());
System.out.println(bigData.search(30));
bigData.sort();
System.out.println("after sort:" + bigData.toString());
}
}
class BigData {
private int[] number;
public BigData(int[] sample) {
this.number = sample;
}
public boolean search(int data) {
boolean result = false;
for (int i : number) {
if (i == data) {
result = true;
}
}
return result;
}
public void sort() {
int temp;
for (int i = 0; i < number.length; i++) {
for (int j = i + 1; j < number.length; j++) {
if (number[i] < number[j]) {
temp = number[i];
number[i] = number[j];
number[j] = temp; // 讓兩數交換位子
}
}
}
}
public String toString() {
String result = "";
for (int i : number) {
result += "," + i;
}
return result.substring(1);
}
}
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
因為我可能還沒有學到那麼深,有什麼可以替換的方法嗎?
return Arrays.binarySearch(this.number, data) >= 0;(例如這行binarySearch是不是內建的方法?)