62,615
社区成员
发帖
与我相关
我的任务
分享
public class WildCardNeedDemo {
public static void main(String[] args) {
GenericStack<Integer> intStack = new GenericStack<>();
intStack.push(1);
intStack.push(2);
intStack.push(-2);
System.out.println("the max number is: " + max1(intStack));
System.out.println("the max number is: " + max2(intStack));
}
public static <type extends Number> double max1(GenericStack<type> stack) {
double max = stack.pop().doubleValue();
while (!stack.isEmpty()) {
double value = stack.pop().doubleValue();
if (value > max) {
max = value;
}
}
return max;
}
public static double max2(GenericStack<? extends Number> stack) {
double max = stack.pop().doubleValue();
while (!stack.isEmpty()) {
double value = stack.pop().doubleValue();
if (value > max) {
max = value;
}
}
return max;
}
}
public class question {
public static void main(String[] args) {
ArrayList<Integer> intStack = new ArrayList<>();
intStack.add(1);
intStack.add(2);
intStack.add(-4);
System.out.println("the max number is: " + max1(intStack));
Integer max1 = max1(intStack); //区别:这里不需要强行转换
ArrayList<Integer> intStack2 = new ArrayList<>();
intStack2.add(1);
intStack2.add(2);
intStack2.add(-4);
System.out.println("the max number is: " + max2(intStack2));
System.out.println(intStack2.size());
Integer max2 = (Integer)max2(intStack2); //这里需要强行转换,因为没法用?作为返回值类型
}
static <type extends Number> type max1(ArrayList<type> stack) { //这里返回值可以直接用type类型
type max = stack.get(0);
for (type t : stack) { //这里能用type来for循环
if (t.doubleValue() > max.doubleValue()) {
max = t;
}
}
return max;
}
static Number max2(ArrayList<? extends Number> stack){ //这里返回值不能用?作为类型
Number max = stack.get(0);
for (Number n: stack) { //这里不能用?来for循环
if (n.doubleValue() > max.doubleValue()) {
max = n;
}
}
return max;
}
}
public static <type extends Number> type max1(GenericStack<type> stack) { //返回值类型改成type
double max = stack.pop().doubleValue();
while (!stack.isEmpty()) {
double value = stack.pop().doubleValue();
if (value > max) {
max = value;
}
}
return stack.pop();
}
public static Number max2(GenericStack<? extends Number> stack) { //你会发现这里的返回值类型不能改成[?],这就是区别
double max = stack.pop().doubleValue();
while (!stack.isEmpty()) {
double value = stack.pop().doubleValue();
if (value > max) {
max = value;
}
}
return stack.pop();
}
public class question {
public static void main(String[] args) {
ArrayList<Integer> intStack = new ArrayList<>();
intStack.add(1);
intStack.add(2);
intStack.add(-4);
System.out.println("the max number is: " + max1(intStack));
ArrayList<Integer> intStack2 = new ArrayList<>();
intStack2.add(1);
intStack2.add(2);
intStack2.add(-4);
System.out.println("the max number is: " + max2(intStack2));
System.out.println(intStack2.size());
}
static <type extends Number> double max1(ArrayList<type> stack) {
double max = stack.remove(stack.size() - 1).doubleValue();
while(!stack.isEmpty()){
double value = stack.remove(stack.size()-1).doubleValue();
if(value>max){
max = value;
}
}
return max;
}
static double max2(ArrayList<? extends Number> stack){
double max = stack.remove(stack.size() - 1).doubleValue();
while(!stack.isEmpty()){
double value = stack.remove(stack.size()-1).doubleValue();
if(value>max){
max = value;
}
}
return max;
}
}