因此该程序尝试采用如下命令行参数:
S 4 1 2 3 4 4
args[0]
是数组类型
args[1]
是数组长度
args[2...]
是数组中的值
args[length-1]
是将在线性搜索中使用的键
public class whatTheFoo{
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <E> void main(String[] args) {
for(int i=0;i<args.length;i++)System.out.print(args[i]);
System.out.println();
int arraySize = Integer.parseInt(args[1]);
E[] array = (E[])new Object[arraySize];
E key = null;
if (args[0].matches("I|i")) {
for (int i = 2; i < args.length-1; i++) {
array[i-2]=(E)new Integer(args[i]);
System.out.println(array[i-2]);
}
key = (E) new Integer(args[args.length-1]);
System.out.println("Key is: " + key);
}
...
if(linearSearch(array, key)<0)
System.out.println("Didnt find it");
else
System.out.println("Found it at index: "+(linearSearch(array, key)-1));
}
public static <E> int linearSearch(E[]array,E key) {
int index=-1;
for(int i=0;i<array.length;i++) {
if(array[i].equals(key)){
index = (int) array[i];
}
}
return index;
}
}
这可行,但是当我将 LinearSearch 方法更改为:
public static <E extends Comparable<E>> int linearSearch(E[]array,E key)
我收到错误消息:
The method linearSearch(E[], E extends Comparable<E>) in the type Prog7b is not applicable for the arguments (E[], E)
但是如果我将 main 更改为:
public static <E extends Comparable<E>> void main(String[] args) {
我得到:
Exception in thread "main" I412344java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
at whatTheFoo.main(whatTheFoo.java:10)
该方法已被指示包含在方法中:
<E extends Comparable<E>>.
我哪里出错了?谢谢阅读。
-------------------------------------------------------------------
对于那些可能好奇的人,这是提供的所有帮助的最终结果。再次感谢!
public class Prog7b {
// @SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
if (args[0].matches("I|i")) {
Integer[] array = new Integer[Integer.parseInt(args[1])];
for (int i = 2; i < args.length - 1; i++) {
array[i - 2] = new Integer(args[i]);
}
Integer key = new Integer(args[args.length - 1]);
if (linearSearch(array, key) < 0) {
System.out.println("Didnt find it");
} else
System.out.println("Found it at index: " + (linearSearch(array, key) - 1));
System.out.println("The max of the array is: " + max(array));
print(array);
} else if (args[0].matches("S|s")) {
String[] array = new String[Integer.parseInt(args[1])];
for (int i = 2; i < args.length - 1; i++) {
array[i - 2] = new String(args[i]);
}
String key = new String(args[args.length - 1]);
if (linearSearch(array, key) < 0) {
System.out.println("Didnt find it");
} else
System.out.println("Found it at index: " + (linearSearch(array, key) - 1));
System.out.println("The max of the array is: " + max(array));
print(array);
} else {
Double[] array = new Double[Integer.parseInt(args[1])];
for (int i = 2; i < args.length - 1; i++) {
array[i - 2] = new Double(args[i]);
}
Double key = new Double(args[args.length - 1]);
if (linearSearch(array, key) < 0) {
System.out.println("Didnt find it");
} else
System.out.println("Found it at index: " + (linearSearch(array, key) - 1));
System.out.println("The max of the array is: " + max(array));
print(array);
}
}
public static <E extends Comparable<E>> int linearSearch(E[] array, E key) {
int index = 0;
for (int i = 0; i < array.length; i++) {
index++;
if (array[i].equals(key)) {
return index;
}
}
return -1;
}
public static <E extends Comparable<E>> E max(E[] list) {
E max = list[0];
for (int i = 1; i < list.length; i++) {
if (max.compareTo(list[i]) < 0) {
max = list[i];
}
}
return max;
}
private static <E> void print(E[] list) {
System.out.print("[");
for (int i = 0; i < list.length - 1; i++)
System.out.print(list[i] + ", ");
System.out.print(list[list.length - 1] + "]\n");
}
}
最佳答案
我不认为main
应该是通用的。 (方法声明中的 <E>
部分声明了一个类型变量,这使其成为泛型变量。)如果 main
确实应该是通用的,那么你需要和你的老师谈谈,因为他们正在做一些奇怪的事情,我们无法真正猜测。
泛型只是编译时的概念。基本上,这个想法是,您有一些代码实际上对特定类型有些不可知,但仍然需要某种关于它的抽象信息。
例如,假设我们有一些方法来检查对象是否为 null:
Object requireNonNull(Object obj) {
if (obj == null) {
throw new NullPointerException();
} else {
return obj;
}
}
这很好。我们可以将任何类型的对象传递给该方法。 ( Integer
, String
,等等。)但是如果我们想直接分配返回值怎么办?
我们希望能够做到这一点:
String mightBeNull = ...;
String definatelyNotNull = requireNonNull(mightBeNull);
这使我们的验证代码更加整洁。 (也许我们的验证实际上大约有 10 行长,而不是检查 null,并且我们不想一直重复它。)
好吧,就目前情况而言,我们不能,因为尝试分配 Object
时会出现编译时错误。到 String
.
不过,泛型让我们可以做到这一点:
<T> T requireNonNull(T obj) {
if (obj == null) {
throw new NullPointerException();
} else {
return obj;
}
}
类型参数<T>
说我们声明了一种临时类型。我们不关心它实际上是什么,但我们可以说该方法返回我们传递给它的任何内容。无论类型 obj
就是我们调用requireNonNull
的点,该方法将该类型返回给调用者。
现在我们可以这样做:
String s = requireNonNull("");
Integer i = requireNonNull(10);
Float f = requireNonNull(2f);
等等。
requireNonNull
实际上是一个真正的方法,这就是它的工作原理。
不过,重点是泛型让您可以编写非常通用的 API,该 API 会被非泛型代码调用。
对于您的作业,您似乎应该编写一个通用方法 linearSearch
具有有界类型参数 <E extends Comparable<E>>
(本质上意味着无论您传递给 linearSearch
的数组类型是什么,它都必须是 Comparable
的某种子类型)。那么你可能应该在 main
中向它传递不同类型的数组,例如Integer[]
, String[]
等您的main
方法不会是通用的。你只会有一个 if...else if
args[0]
每种类型的链需要。
关于Java:<E 扩展了 Comparable <E>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43648174/