-1

I added import java.util.ArrayList; (per suggestions here) to my code and then I got 2 different kinds of errors. 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.

6
  • 3
    what is this mysterious code? Commented Mar 28, 2011 at 15:57
  • 2
    @jzd - It was suggested on that question that the OP open a new question for this issue, rather than reusing the old one. Commented Mar 28, 2011 at 16:06
  • 1
    @Leasha - Like Jules requested in his answer, it helps to post relevant code snippets with your questions. I'm going to guess that you've made some changes to the code that you originally put in this question. Perhaps you can provide us with the code changes you've made to that so we can help you identify what's wrong with it. Commented Mar 28, 2011 at 16:19
  • Can I email someone with my full code that is willing to help me? Commented Mar 28, 2011 at 16:33
  • Similar recent post from this OP: stackoverflow.com/questions/5461395/java-error-code Commented Mar 28, 2011 at 16:35

2 Answers 2

4

For an ArrayList you need to call the .size() Method to get the number of elements, not length. You can't just treat the ArrayList like a basic array. 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?

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/

If you want to experiment with an ArrayList, look up which methods and properties it has. Usually, it also links to proper tutorials like, e.g. 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.

Sign up to request clarification or add additional context in comments.

3 Comments

import java.util.ArrayList; // sets up random number of markers in a // one-dimensional array // numMarkers markers in a board of size boardSize public class SimpleDotCom { // constants private final static int DEFAULT_MARKERS = 3; private final static int DEFAULT_BOARD_SIZE = 10; // data members private ArrayList<Integer> markers; // stores the marker positions private int boardSize; // stores the size of the board private int endOfMarkers;
that is part of my code. I don't know if it is useful or not for these errors
@Leasha: Please edit your original post and provide the code in a reasonable formatting. But from what I can see this homework is encouraging you to use basic arrays and not higher level collection classes (private int endOfMarkers).
1

For your second error, it seems like you are trying to use arrays and Lists interchangeably, although they are two separate data types.

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. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.