sách gpt4 ai đã đi

java - 如何使用 -Xlint 重新编译?掌握

In lại 作者:行者123 更新时间:2023-11-30 07:09:37 36 4
mua khóa gpt4 Nike

我的程序似乎很合我意。但是,当我编译它时,我收到了这条消息:

Note: Program.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

我可以做什么来识别使用 -Xlint 的不安全操作,或者程序中的什么导致了此消息?我认为这与我的 Node 类有关......?

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;

/**
* An application that reads from a file, enters/deletes in queue and writes output to the file
*/
public class Program {

/**
* Driver code to test class
*
* @param arguments
* Commandline arguments. 1st argument is input file and 2nd argument is output file
* @throws IOException
*/
public static void main(String[] arguments) throws IOException {


//Queue Object
MyQueue queue= (new MyQueue());

Tên chuỗi;
//reading file
read(queue,arguments[0]);

String[] array = { "Offer Person", "Poll Person", "Peek person","Display Queue", "Exit Program"};
int choice = 0;

// display loop
while (choice != array.length-1) {
choice = JOptionPane.showOptionDialog(null, // put in center of screen
"Press a Button", // message to user
"Queue(Line) of People", // title of window
JOptionPane.YES_NO_CANCEL_OPTION, // type of option
JOptionPane.QUESTION_MESSAGE, // type of message
null, // icon
array, // array of strings
array[array.length - 1]); // default choice (last one)


if(choice==0)
{
//inserting the new name in queue
name=JOptionPane.showInputDialog(null,"Enter Person's name","Input");
queue.offer(name);

}
else if(choice==1){

//Display and remove the name which is at front of line
JOptionPane.showMessageDialog(null, queue.poll() + " is next in line");

}

else if(choice==2){

//Display name which is at front of line
JOptionPane.showMessageDialog(null, queue.peek() + " is front of the line");

}

else if(choice==3){
//Dispay all the list
JOptionPane.showMessageDialog(null, queue.toString());


}
//JOptionPane.showMessageDialog(null, "Your pressed button #" + choice);
}
//calling writing function
write(queue, arguments[1]);


}// end of main()

/**
* Reads a file
* @param queue
* @param file_name name of file
*/
public static void read(QueueInterface queue, String file_name) throws IOException{

thử
{
Tên chuỗi;
//creating a buffer reader to read
BufferedReader br= new BufferedReader(new FileReader(file_name));
while((name=br.readLine()) != null){
//putting in the queue
queue.offer(name);
}
//closing buffer reader
br.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}

}

/**
* Writes the contents of LinkedQueue to the output file at the ned of program
* @param queue QueueInterface methods
* @param file_name name of file
*/
public static void write(QueueInterface queue, String file_name) throws IOException{
thử
{
Tên chuỗi;
//creating a buffer writer to write
BufferedWriter bw= new BufferedWriter(new FileWriter(file_name));
while((name=queue.poll()) != null){
//writin in file
bw.write(name);
bw.newLine();

}
//closing buffer
bw.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}



}// end of class

/**
* Interface to be implemented by LinkedQueue
*/
interface QueueInterface
{
public boolean empty();
public boolean offer(String element);
public String poll();
public String peek();
}

class Node
{
private String data;
private Node nextNode;
public Node(String dataObject, Node nextNodeObject)
{
this.data=dataObject;
this.nextNode=nextNodeObject;
}

/**
* Gets the next node
* @return next node
*/
public Node getNext()
{
return nextNode;
}

/**
* Sets the next node of the current node
* @param nextNodeObject next node to be set as next to the current node
*/
public void setNext(Node nextNodeObject)
{
nextNode=nextNodeObject;
}

/**
* Sets data of the current node
* @param dataObject data to be inserted in new node
*/
public void setData(String dataObject)
{
this.data=dataObject;
}

/**
* Gets data of the current node
* @return data of the node
*/
public String getData()
{
return this.data;
}
}

class LinkedQueue implements QueueInterface
{
protected Node lastNode=null;

LinkedQueue() {
}

/**
* Checks if the queue is empty
* @return true if empty, false if not empty
*/
public boolean empty() {
if(lastNode==null)
{
trả về giá trị đúng;
}
khác
trả về false;
}

/**
* Inserts new node in the queue
* @param element data to be inserted in new node
* @return true on success
*/
public boolean offer(String element)
{
Node newLastNode = new Node(element,null);

//If the LinkedQueue is empty, add the node to the last and point next to itself
if(empty())
{
newLastNode.setNext(newLastNode);
}
khác
{
// Adding to the front of queue and updating next of the last node
newLastNode.setNext(lastNode.getNext());
lastNode.setNext(newLastNode);
}
lastNode=newLastNode;
trả về giá trị đúng;
}

/**
* Removes the first node and returns it
* @return data at first node
*/
public String poll()
{
// If queue is empty then return null
if(empty())
trả về giá trị null;
khác
{
Node frontNode = lastNode.getNext();

//Check if there will be no node left after polling this one
if (frontNode == lastNode)
{
lastNode = null;
}
else //Remove the first node and update next of the last node
{
lastNode.setNext(frontNode.getNext());
}
return frontNode.getData();
}
}

/**
* Returns data of the first node without removing it from the queue
* @return data at first node
*/
public String peek()
{
if (empty())
{
trả về giá trị null;
}
khác
{
Node frontNode = lastNode.getNext();
return frontNode.getData();
}
}
}

class MyQueue extends LinkedQueue{

/**
* Constructor
*
*/
public MyQueue()
{
siêu();
}

/**
* Returns a string representation of the object
*
* @return a name on different lines
*/
public java.lang.String toString()
{
// create a variable to return
java.lang.String toReturn = "";

// Traversing the list
Node frontNode = lastNode.getNext();

if(empty()) //If queue is empty
return "";
else if(frontNode==lastNode) //If only one elemtn
{
return frontNode.getData().toString();
}
else //More than one element in the queue
{
while(frontNode != lastNode)
{
toReturn=toReturn+frontNode.getData()+"\n";
frontNode=frontNode.getNext();
}
toReturn= toReturn+frontNode.getData()+"\n"; //Appending data of last node because it will be missed in the loop
}
return toReturn;
}
}

1 Câu trả lời

如果您在命令行上编译(即 javac Program.java ),您只需添加 -Xlint:unchecked让它为您打印警告的参数:

javac Program.java -Xlint:unchecked

这应该会向您指出问题点。但是,正如@DavidWallace 在评论中提到的那样,您应该考虑修改对泛型的使用,使其更加清晰——即使不使用 -Xlint 也可能会向您揭示您的问题。参数。

如果你的类真的应该只处理 Sợi dây s,那么你根本不需要包含类型参数(现在,在你的代码中, 是一个类型参数,代表你在使用类时传入的类型 - 它并不意味着它必须是 java.lang.String——这就是为什么@DavidWallace 建议您改用 T 的原因)。 Here's如果您想复习如何使用泛型,这是一个很好的教程。

关于java - 如何使用 -Xlint 重新编译?掌握,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22875001/

36 4 0
Bài viết được đề xuất: javascript - 如何进行多个正则表达式替换
Bài viết được đề xuất: java - 如何从 Rest 调用返回 List
Bài viết được đề xuất: java - Joda-Time getMillisOfDay() 似乎比 java.util.Date 的 getTime() 毫秒值进步得更快
Bài viết được đề xuất: java - 选择列表转换器
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com