Joh*_*nny 0 java eclipse undefined
我正在进行一项任务,我正在尝试向LinkedList添加一个元素.第一个代码块是给定的,不应该更改.第二个块是根据教授给我们的UML编写的,位于另一个类中.
import java.io.*;
import java.util.LinkedList;
public class Assignment10
{
public static void main(String[] args)
{
char input1;
String inputInfo = new String();
int operation2;
String line = new String();
//create a linked list to be used in this method.
LinkedList list1 = new LinkedList();
try
{
// print out the menu
printMenu();
// create a BufferedReader object to read input from a keyboard
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (isr);
do
{
System.out.print("What action would you like to perform?\n");
line = stdin.readLine().trim(); //read a line
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1) // check if a user entered only one character
{
switch (input1)
{
case 'A': //Add String
System.out.print("Please enter a string to add:\n");
String str1 = stdin.readLine().trim();
System.out.print("Please enter an index to add:\n");
inputInfo = stdin.readLine().trim();
int addIndex = Integer.parseInt(inputInfo);
list1.addElement(addIndex, str1);
break;
public void addElement(int index, Object element)
{
if(index < 0)
{
IndexOutOfBoundsException ex = new IndexOutOfBoundsException();
throw ex;
}
LinkedListIterator iterator = new LinkedListIterator();
for(int i = 0; i < index; i++)
{
if(iterator.hasNext()) // check if the iterator has a next value before
iterator.next(); // moving to next element
else
{
NoSuchElementException exception = new NoSuchElementException();
throw exception;
}
}
iterator.add(element);
} // end of addElement
Run Code Online (Sandbox Code Playgroud)
以下是Eclipse告诉我的内容:线程"main"中的异常java.lang.Error:未解决的编译问题:对于LinkedList类型,方法addElement(int,String)是未定义的.
同样,我不应该更改第一个代码块,因此我的addElement方法一定有问题.有任何想法吗?对不起,这是不可编译的,但我认为这更像是一个概念性问题.
我认为问题在于有人感到困惑.
一方面,您似乎正在实现一个名为addElement自定义列表实现的方法.(你没有向我们展示整个班级...)
另一方面,您似乎试图addElement在标准java.util.LinkedList类上调用不存在的东西.
其中一件事显然是错误的.要么你误解了你应该做什么,要么你的讲师给你一个Assignment10不正确的测试工具类().(是的讲师确实犯了错误.他们只是人类.)
我建议你让你的讲师或导师澄清一下.(并且请礼貌和恭敬.对于讲师而言,没有什么比"面对面"错误的学生更烦人,特别是想象中的错误.)
| 归档时间: |
|
| 查看次数: |
2422 次 |
| 最近记录: |