Re: Error:Cannot find Symbol
The ArrayList class is in a package. You need an import statement that tells the compiler the name of that package.
Re: Error:Cannot find Symbol
Don't forget to import java.util.ArrayList.
Re: Error:Cannot find Symbol
Nvm fixed that one too
--- Update ---
Alright my code compiled without a problem. Thank you guys
--- Update ---
Code java:
//This class will take four grades then display them, remove the first element and display the new Array and Display the Array size and the second object.
import java.util.ArrayList; //Imports Arraylist
public class Gradebook // Main class
{
public static void main(String []args) //Main method
{
ArrayList<Integer> Grades = new ArrayList<Integer>(); //creates a new Array;ist
Grades.add(100); //Adds 100 to element 0
Grades.add(90); // Adds 90 to element 1
Grades.add(80); // Adds 80 to element 2
Grades.add(70); //Adds 70 to element 3
for(Integer element : Grades){ //The enchanced for loop statement
}
for (int i = 0; i <Grades.size(); i++) //Sets up a break between each number
{
if (i > 0)
{
System.out.print("|"); //The break to make it easier to read
}
System.out.println(Grades.get(i)); //Displays all the grades
}
Grades.remove(0); //Removes the first grade
System.out.println(Grades); //Displays the new Grade
int n = Grades.size(); //Puts the ArrayLists number of elements into int n
System.out.printf("%d",n);//Displays int n, which is the number of elements
System.out.println(Grades.get(1)); //Displays the second object
}
}
--- Update ---
Yeah new problem came up. When I want to access the second object in the Arraylist, which would be element 1, I am getting 380...Do you know why i am getting that?
--- Update ---
Am I getting the reference number maybe?
--- Update ---
Nvm. I was being stupid forgot that 3 and 80 weren't being split up.
Re: Error:Cannot find Symbol
You will want to improve your code formatting using consistent indentation. Your for loops especially are very difficult to read and it's hard to know what code is inside and what is outside of the for loop. Please edit your code above as this will help both you and us.
It sort of appears that you're removing an element from the ArrayList from within the for loop, and if so, don't do that as you'll get unexpected side effects. For one if you remove the 0th element while its looping, the next element you get an the next iteration of the loop won't be the next item in the ArrayList because the list has changed.
Edit: no you're not removing items in the loop so ignore that, but again please please fix your indenting. It shows that you respect our efforts and free time if you put in efforts to make it easier for us to read your code.