折腾一天了,指导一下
编写一个泛型类,适合于基本数据类型byte、short、int、long、float、double数组的排序与求和
这个一部分题目,这个部分我用的重载,因为使用泛型死活不成功。
package experiment6;
import java.io.*;
import java.util.*;
public class Types<T> {
T[] sorts(T[] temp) {
Arrays.sort(temp);
return temp;
}
String plus(String[] temp) {
String result = new String();
for (int i = 0; i < temp.length; i++) {
result += temp[i];
}
return result;
}
double plus(double[] temp) {
double result = 0;
for (int i = 0; i < temp.length; i++) {
result += temp[i];
}
return result;
}
Integer plus(Integer[] temp) {
Integer result = 0;
for (int i = 0; i < temp.length; i++) {
result += temp[i];
}
return result;
}
short plus(short[] temp) {
short result = 0;
for (int i = 0; i < temp.length; i++) {
result += temp[i];
}
return result;
}
byte plus(byte[] temp) {
byte result = 0;
for (int i = 0; i < temp.length; i++) {
result += temp[i];
}
return result;
}
float plus(float[] temp) {
float result = 0;
for (int i = 0; i < temp.length; i++) {
result += temp[i];
}
return result;
}
long plus(long[] temp) {
long result = 0;
for (int i = 0; i < temp.length; i++) {
result += temp[i];
}
return result;
}
List<T> extract(T[] temp,int n) {
Random rand = new Random();
boolean[] bool = new boolean[temp.length];
int t;
List<T> lists = new ArrayList<T>();
for (int i = 0; i < n; i++) {
do
t = rand.nextInt(temp.length);
while (bool[t]);
lists.add(temp[t]);
bool[t] = true;
}
return lists;
}
}
class UseTypes {
Scanner scanner = new Scanner(System.in);
int temp;
void score() {
System.out.println("请输入一组学生成绩,求平均数。");
System.out.print("请输入学生个数:");
temp = scanner.nextInt();
double[] score = new double[temp];
for (int i = 0; i < temp; i++) {
System.out.print("请输入成绩:");
score[i] = scanner.nextDouble();
}
Types<Double> types = new Types<Double>();
System.out.printf("学生平均成绩为: %.1f\n",types.plus(score)/temp);
}
void draw() {
System.out.println("请输入一组学生学号进行抽奖。");
System.out.println("请输入要抽取的学生个数:");
temp = scanner.nextInt();
System.out.println("请输入要中奖学生个数:");
int n = scanner.nextInt();
String[] id = new String[temp];
for (int i = 0; i < id.length; i++) {
id[i] = scanner.next();
}
Types<String> types = new Types<String>();