5

I am trying to make an array list in Java in two spots, but I don't know what I am doing wrong. It says an array is required, but I don't know what that means because I am using an array list.

This is the line that's being messed up:

static char rSpaces(String problem, int count)
{
    num.add(problem.charAt(count));
    char no = num[count];
    return no;
}

If this helps, this is the line I created the array list in (I already imported it):

static ArrayList<Character> num = new ArrayList<Character>();

5 Answers 5

5

num[count] is wrong, since num is not an array. Use num.get(count) instead.

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

Comments

2

An ArrayList is not an array, so you can't use the array element [] syntax here.

With an ArrayList, use the get method to access an element.

Comments

2

You should use ArrayList.get to access the elements of an ArrayList. Change that to:

  char no = num.get(count);

Comments

0

Java array and ArrayList are different things.

You could access the size of the ArrayList by using method size as follows:

static char rSpaces(String problem, int count)
{
    num.add(problem.charAt(count));
    char no = num.get(count);
    return no;
}

If you want to access it as an array, you could "export" it using the toArray method as follows:

...
Character[] myArray = num.toArray(new Character[]{})
Character c = myArray[count];
...

Comments

0

To access element of array use index operation using [] operator num[count] while in case of ArrayList you need to use get(count) method.

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.