简体   繁体   English

Dr java中的java错误

[英]java errors in dr java

I added import java.util.ArrayList; 我添加了import java.util.ArrayList; ( per suggestions here ) to my code and then I got 2 different kinds of errors. 根据此处的建议 ),然后出现2​​种不同类型的错误。 They are: 他们是:

error: the type of the expression must be an array type but it resolved to java.util.ArrayList<java.lang.Integer>

and: 和:

error: length cannot be resolved or is not a field.

Can anyone tell me what they mean? 谁能告诉我他们的意思吗? I've tried changing the length statements by adding () at the end but that cause more errors then I started with. 我尝试通过在末尾添加()更改length语句,但这会导致比我开始时出现的更多错误。

For an ArrayList you need to call the .size() Method to get the number of elements, not length. 对于ArrayList,您需要调用.size()方法以获取元素数,而不是长度。 You can't just treat the ArrayList like a basic array. 您不能仅仅将ArrayList视为基本数组。 Please provide some code samples for help with the other error. 请提供一些代码示例以帮助解决其他错误。

int i = myList.size();

EDIT: 编辑:

I've just seen, that someone actually mentioned that already in another question of yours. 我刚刚看到,实际上有人在您的另一个问题中提到了这一点。 How to modify a java program from arrays to arraylist objects? 如何将Java程序从数组修改为arraylist对象?

Since you try to employ a method of community-coding just some tips for you to grow and the community to save some nerves ;) 由于您尝试使用社区编码的方法,因此仅提供一些技巧,可以帮助您成长,并为社区节省一些麻烦;)

Try to keep the JavaDoc open in a browser while you code: http://download.oracle.com/javase/6/docs/api/ 在编写代码时,尝试使JavaDoc在浏览器中保持打开状态: http : //download.oracle.com/javase/6/docs/api/

If you want to experiment with an ArrayList, look up which methods and properties it has. 如果要尝试使用ArrayList,请查找其具有的方法和属性。 Usually, it also links to proper tutorials like, eg how to use collection classes. 通常,它还会链接到适当的教程,例如如何使用集合类。 It's probably quicker to browse the JavaDoc rather than posting a question here and it will give you a good general picture of basic Java classes. 浏览JavaDoc可能要比在此处发布问题更快,这将使您对基本Java类有一个大致的了解。

For your second error, it seems like you are trying to use arrays and List s interchangeably, although they are two separate data types. 对于第二个错误,尽管它们是两个单独的数据类型,但您似乎正试图互换使用数组List

In your code, you probably have something like: 在您的代码中,您可能会有类似以下内容:

int[] myArray = new ArrayList<Integer>();

You need to figure out which type you want to use. 您需要确定要使用的类型。 If you need an array, use: 如果需要数组,请使用:

int[] myArray = new int[0]; // replacing 0 with your initial array size

If you need a list, use: 如果需要列表,请使用:

List<Integer> myList = new ArrayList<Integer>();

As you've mentioned before, you're new to Java. 如前所述,您是Java的新手。 You might find value in reading through some of the basic tutorials ; 通过阅读一些基本教程 ,您可能会发现价值; they may give you a better understanding of what's happening with your code. 它们可以使您更好地了解代码所发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM