Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ArrayList Syntax Error

Tags:

java

arraylist

I'm getting an error when declaring this ArrayList as an instance variable in Java.

private ArrayList<char> correctGuesses = new ArrayList<char>();

The error:

Syntax error on token char, Dimension expected after this token

Can I not make ArrayLists with type char?

like image 592
Chris Avatar asked Jul 17 '11 21:07

Chris


People also ask

What is the syntax of ArrayList?

In Java, we can create ArrayList by creating this simple statement: ArrayList<String> arlist = new ArrayList<String>( ); In above syntax, list is of “String” type, so the elements are that going to be added to this list will be string type. The type decide which type of elements list will have.

Can you resize an ArrayList in Java?

The difference between an array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can be added/appended or removed from an ArrayList without the need to create a new array.

What is the function of ArrayList in Java?

ArrayList in Java is used to store dynamically sized collection of elements. Contrary to Arrays that are fixed in size, an ArrayList grows its size automatically when new elements are added to it. ArrayList is part of Java's collection framework and implements Java's List interface.

What is ArrayList in Java?

Java ArrayList. The ArrayList class is a resizable array, which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one). While elements can be added and removed ...

Is this a compliance error in ArrayList?

It's not only a compliance error. It's the way a List ist declared Show activity on this post. Specify the type of elements that will be stored in the ArrayList. In this case I think you are storing integer values in size list, so just define Integer type inside angle brackets like this:

Should I use ArrayList instead of ArrayList when declaring variables?

In addition, as gundev added to this answer, best practice is to use the List interface type rather than the ArrayList type when declaring these types of variables This is just like how you specify types for arrays, and other variables, but with a slightly different syntax.

How to add strings to an ArrayList in Java?

Create an ArrayList object called cars that will store strings: If you don't know what a package is, read our Java Packages Tutorial. The ArrayList class has many useful methods. For example, to add elements to the ArrayList, use the add () method: To access an element in the ArrayList, use the get () method and refer to the index number:


1 Answers

You can't use a primitive type, rather you use its Wrapper class.. So instead of char you would have Character

ArrayList<Character> correctGuesses = new ArrayList<Character>();
like image 190
DaMainBoss Avatar answered Sep 30 '22 20:09

DaMainBoss