ArrayList String problem


#1

https://www.codecademy.com/courses/learn-java/lessons/data-structures/exercises/generalizations-data-structures?action=resume_content_item

What am i doing wrong cant open ArrayList im using this code:

ArrayList <String> sports= new ArrayList<String>;

and i allways get this error:

RestaurantForEach.java:14: error: ‘(’ or ‘[’ expected
ArrayList sports= new ArrayList;
^
1 error


#2

I’m not versed in Java but should the array have an assigned data type,

Arraylist<String>

or some such?


#3

it has? sports of type ArrayList which contains strings. But just like with javascript using the new keyword, like new Object() parentheses are requires at the end.

the error message says as much:

but like you, my Java knowledge isn’t great.


#4

strings in java are characters by default as i understood, if you wana add a number you set int of flow or something, if you wana put just a character you put char


#5

i tryed to use but then i get 2 errors

RestaurantForEach.java:14: error: not a statement
ArrayList() sports= new ArrayList();
^
RestaurantForEach.java:14: error: ‘;’ expected
ArrayList() sports= new ArrayList();
^
2 errors


#6

OP must have formatted his post after my reply.

Concur…

ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();

#7

now you are just trying with understanding what you are doing


#8

it is if you wana use integer i have to put them but for strings is something different i guess.

ive copied the statement from the previous example with int it doesnt work!

RestaurantForEach.java:14: error: cannot find symbol
ArrayList sports= new ArrayList();
^
symbol: class ArrayList
location: class RestaurantForEach
RestaurantForEach.java:14: error: cannot find symbol
ArrayList sports= new ArrayList();
^
symbol: class ArrayList
location: class RestaurantForEach
2 errors


#9

Maybe you have an error somewhere else? Please post your full code. Use format as explained here:

How do I format code in my posts?

to ensure all your code shows properly


#10

so i can use this for java code to?

I found the solution i restarted the web page and wrote this code and it got accepted after all.

ArrayList sports= new ArrayList();


#11

what is this referring to? I always attempt to avoid use words like this to prevent confusion. Without format your code looks like:

ArrayList sports= new ArrayList();

with format:

ArrayList <String> sports= new ArrayList<String>;

quite a difference, no?


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.