Java 使用-Xlint重新编译代码:未选中。使用-Xlint重新编译后,将显示未选中调用警告。我现在如何运行我的程序?
Java引入了(相当晚)集合项的类型,如Java 使用-Xlint重新编译代码:未选中。使用-Xlint重新编译后,将显示未选中调用警告。我现在如何运行我的程序?,java,Java,Java引入了(相当晚)集合项的类型,如对象的Vector作为Vector,其中T是给定的类型参数,如Vector。(这称为泛型输入。) 现在编译器可以“检查”项目类型是否正确 /** traversing elements */ import java.util.*; class Traversor extends Thread { Enumeration e; public Traversor(Enumeration e) { this.e=e; } public void run()
对象的Vector
作为Vector
,其中T
是给定的类型参数,如Vector
。(这称为泛型输入。)
现在编译器可以“检查”项目类型是否正确
/** traversing elements */
import java.util.*;
class Traversor extends Thread
{
Enumeration e;
public Traversor(Enumeration e)
{
this.e=e;
}
public void run()
{
System.out.println("new thread started, traversing vector elements.....");
while(e.hasMoreElements())
{
System.out.println(e.nextElement());
try
{
Thread.sleep(4000);
}
catch(Exception ex)
{}
}
System.out.println("new thread completed");
}
}
class vectortest{
public static void main(String args[])
{
System.out.println("main thread started, creating vector...");
Vector v=new Vector();
v.addElement("one");
v.add("three");
v.add(1,"two");
Enumeration e=v.elements();
System.out.println("vector created ,enumeration obtained");
Traversor th=new Traversor(e);
th.start();
System.out.println(" new thread launched , suspending main thread");
try{
Thread.sleep(1000);
}
catch(Exception ex)
{}
System.out.println("main thread resumed,modifying vector");
v.add("four");
v.add("five");
System.out.println("vector modified, main thread completed");
}
}
类遍历器扩展线程{
e;
公共遍历器(枚举e){
这个。e=e;
}
公开募捐{
System.out.println(“新线程启动,遍历向量元素…”);
而(e.hasMoreElements()){
System.out.println(e.nextElement());
试一试{
睡眠(4000);
}捕获(例外情况除外){
}
}
System.out.println(“新线程完成”);
}
}
类向量测试{
公共静态void main(字符串参数[]){
System.out.println(“主线程启动,创建向量…”);
向量v=新向量();
v、 附录(“一”);
v、 添加(“三”);
v、 添加(1,“两”);
枚举e=v.elements();
System.out.println(“创建向量,获得枚举”);
遍历器th=新遍历器(e);
th.start();
System.out.println(“新线程启动,挂起主线程”);
试一试{
睡眠(1000);
}捕获(例外情况除外){
}
System.out.println(“主线程恢复,修改向量”);
v、 添加(“四”);
v、 添加(“五”);
System.out.println(“向量修改,主线程完成”);
}
}
顺便说一下,向量和枚举是相当古老的。其实现在很少见到矢量
class Traversor<T> extends Thread {
Enumeration<T> e;
public Traversor(Enumeration<T> e) {
this.e = e;
}
public void run() {
System.out.println("new thread started, traversing vector elements.....");
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
try {
Thread.sleep(4000);
} catch (Exception ex) {
}
}
System.out.println("new thread completed");
}
}
class VectorTest {
public static void main(String args[]) {
System.out.println("main thread started, creating vector...");
Vector<String> v = new Vector<>();
v.addElement("one");
v.add("three");
v.add(1, "two");
Enumeration<String> e = v.elements();
System.out.println("vector created ,enumeration obtained");
Traversor<String> th = new Traversor<>(e);
th.start();
System.out.println(" new thread launched , suspending main thread");
try {
Thread.sleep(1000);
} catch (Exception ex) {
}
System.out.println("main thread resumed,modifying vector");
v.add("four");
v.add("five");
System.out.println("vector modified, main thread completed");
}
}
//矢量的替代方案:
列表=新的ArrayList();
列表。添加(“alpha”);
对于(字符串s:list){…}
//枚举的替代方案:
迭代器迭代器=list.Iterator();
while(iterator.hasNext()){
字符串s=迭代器.next();
}
请更具体地回答您的问题。此外,您似乎正在使用过时的类Vector
和Enumeration
,而不是ArrayList
和Iterator
,您是否正在学习一些非常古老的教程/书籍?请求调试帮助的问题应包含一个示例,并包括所需的行为。这没有提供任何东西!而且,这段代码完全不可读。适当缩进。
// Alternative to Vector:
List<String> list = new ArrayList<>();
list.add("alpha");
for (String s : list) { ... }
// Alternative to Enumeration:
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String s = iterator.next();
}