adding elements into Arraylist outside of main method
Is there any way to declare my arraylist outside of the main method? The code below is giving me an error saying "Syntax error, on token ";", @ expected."
import java.util.ArrayList;
public class ArrayListPractice {
static ArrayList<Integer> x = new ArrayList<Integer>();
x.add(0);
x.add(50);
x.add(100);
public static void main(String args[]){
System.out.println(x);
}
}
I can solve this if I have all the statements inside my main method then it works and prints out the array to the console. Problem is that the array wouldn't be available to other methods in a larger program. For example in the code below, x cannot be resolved.
import java.util.ArrayList;
public class ArrayListPractice {
static void printSecondElement(){
System.out.println(x.get(1));
}
public static void main(String args[]){
ArrayList<Integer> x = new ArrayList<Integer>();
x.add(0);
x.add(50);
x.add(100);
System.out.println(x);
printSecondElement();
}
}
How can I go about solving this? The best answer will explain how to do it both ways. That is by declaring the arraylist in the main method versus outside of the main method, all the while making sure x is available to other methods. Though I'd appreciate any answer, especially if it's only possible to do it one way.
Solution 1:
This will work
import java.util.ArrayList;
public class ArrayListPractice {
public static ArrayList<Integer> x = new ArrayList<Integer>();
static void printSecondElement(){
System.out.println(x.get(1));
}
public static void main(String args[]){
x.add(0);
x.add(50);
x.add(100);
System.out.println(x);
printSecondElement();
}
}
If you want to move all code outside of main method you need to use a static initializer
import java.util.ArrayList;
public class ArrayListPractice {
public static ArrayList<Integer> x = new ArrayList<Integer>();
static {
x.add(0);
x.add(50);
x.add(100);
}
static void printSecondElement(){
System.out.println(x.get(1));
}
public static void main(String args[]){
System.out.println(x);
printSecondElement();
}
}
Solution 2:
This should work just fine:
import java.util.ArrayList;
public class ArrayListPractice
{
static ArrayList<Integer> x = new ArrayList<Integer>();
static void printSecondElement()
{
System.out.println(x.get(1));
}
public static void main(String args[])
{
x.add(0);
x.add(50);
x.add(100);
System.out.println(x);
printSecondElement();
}
}
Solution 3:
No you can't add elements outside methods. However, you create a static method that would add elements to the array for you.
public static void addElement(int i){
x.add(i);
}
This way you can pass an element as a parameter.
Solution 4:
The reason is that x.add(0)
is a processing/execution statement which should be put inside a method like main
.
Declaration statement like ArrayList<Integer> x
can be put outside of method, but should be inside class.
Solution 5:
You can use static
after creating ArrayList
in your own class :
look at my code:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class solution{
Scanner sc = new Scanner(System.in);
static ArrayList<Integer> mylist = new ArrayList<Integer>();
static { //use static because arraylist is storing the elements dynamically
mylist.add(5); // adding the elements in the arraylist
mylist.add(6);
mylist.add(2);
}
public void printList(){ //to perform any operation on the list you hae to create an method
System.out.println(mylist.get(2));
}
}
public class MainClass{
public static void main(String[] args) {
solution s1 = new solution(); //creating object for the solution class
s1.printList(); //calling the method of solution class
}
}