堆栈-未经检查/不安全的操作 [英] Stack - unchecked/unsafe operations

查看:82
本文介绍了堆栈-未经检查/不安全的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试在此处运行此简单程序:

So I'm trying to run this simple program here:

import java.util.*;

class StackDemo
{
    public static void main(String[] args) {
        Stack s = new Stack();
        s.push(5);
        s.push("dog");
        System.out.print(s);
    }
}

StackDemo.java 使用未经检查或不安全的操作。
注意:有关详细信息,请使用 -Xlint:unchecked 重新编译。
处理完成。

StackDemo.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Process completed.

它显示预期结果,即 [5,dog] 但我不明白构建输出窗口中的消息。
这里可能有什么问题?

It displays the expected result, which is "[5, dog]" but I don't understand that message on the Build Output window. What could possibly be wrong here?

推荐答案

堆栈是一个通用类,您可以根据需要使用它来存储特定类型的对象(例如, Stack< String> 将用于存储字符串)。使用裸类而没有类型说明符通常被认为是一种不好的做法,因为您会丢失集合的类型安全性。

Stack is a generic class, which you can use, if you wish, to store objects of a specific type (e.g., Stack<String> would be used to store strings). Using bare classes, without a type specifier is usually considered a bad practice, as you're losing the type safety of the collection.

如果确实存在用例,想要将 5 dog 都存储在堆栈中,则应使用最常见的堆栈定义堆栈两者之间的分母- Object

If there is indeed a usecase where you'd like to store both 5 and "dog" in your stack, you should define your stack with the greatest common denominator between the two - Object:

Stack<Object> s = new Stack<>();

这篇关于堆栈-未经检查/不安全的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆