First of all, for initializing a container you cannot use a primitive type (i.e. int
; you can use int[]
but as you want just an array of integers, I see no use in that). Instead, you should use Integer
, as follows:
ArrayList<Integer> arl = new ArrayList<Integer>();
For adding elements, just use the add
function:
arl.add(1); arl.add(22); arl.add(-2);
Last, but not least, for printing the ArrayList
you may use the build-in functionality of toString()
:
System.out.println("Arraylist contains: " + arl.toString());
If you want to access the i element, where i is an index from 0 to the length of the array-1, you can do a :
int i = 0; // Index 0 is of the first element System.out.println("The first element is: " + arl.get(i));
I suggest reading first on Java Containers, before starting to work with them.
Related Posts:
- What is an instance variable in Java?
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- When to use LinkedList over ArrayList in Java?
- super() in Java
- Convert list to array in Java [duplicate]
- What’s the syntax for mod in java
- What does the Java assert keyword do, and when should it be used?
- How do I make a delay in Java?
- Casting variables in Java
- Initialization of an ArrayList in one line
- How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList
- What’s the C++ version of Java’s ArrayList
- How to sort an ArrayList?
- max value of integer
- What’s the C++ version of Java’s ArrayList
- Create ArrayList from array
- Best way to convert an ArrayList to a string
- “Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 0, Size: 0″ with ArrayList?
- How can I prevent java.lang.NumberFormatException: For input string: “N/A”?
- Best way to convert an ArrayList to a string
- Print ArrayList
- Print ArrayList
- What is the difference between == and equals() in Java?
- How to make a deep copy of Java ArrayList
- how to fix java.lang.IndexOutOfBoundsException
- What does “?” mean in Java?
- Array ArrayList python equivalent
- Java ArrayList copy
- how to iterate in List
- > in java and set their values as we do in a normal int a[i][j] matrix type [duplicate]
- Java – No enclosing instance of type Foo is accessible
- Why I am getting java.lang.AbstractMethodError errors?
- fix java.net.SocketTimeoutException: Read timed out
- How to convert an ArrayList containing Integers to primitive int array?
- How to override equals method in Java
- Print ArrayList
- java.text.ParseException: Unparseable date
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- HashSet vs. ArrayList
- Java ArrayList replace at specific index
- Java – String cannot be converted to int
- java.io.IOException: Broken pipe
- How can I create an Array of ArrayLists?
- java.util.NoSuchElementException: No line found
- How to create an 2D ArrayList in java?
- Even though JRE 8 is installed on my MAC -” No Java Runtime present,requesting to install ” gets displayed in terminal
- Error: cannot find symbol ArrayList
- Reason for the exception java.lang.VerifyError: Bad type on operand stack
- The specified DSN contains an architecture mismatch between the Driver and Application. JAVA
- java howto ArrayList push, pop, shift, and unshift
- Downcasting in Java
- Java “params” in method signature?
- How do I time a method’s execution in Java?
- How to get the last value of an ArrayList
- repaint() Method in Java
- How to add an object to an ArrayList in Java
- Add String Array to ArrayList
- Exception in thread “main” java.lang.reflect.InvocationTargetException
- How to set String’s font size, style in Java using the Font class?
- Incompatible types List of List and ArrayList of ArrayList
- How to use the command update-alternatives –config java
- java array error “array required but int found”
- this: Cannot use this in static context
- What’s the difference between SoftReference and WeakReference in Java?
- How to draw lines in Java
- NoClassDefFoundError in Java: com/google/common/base/Function
- Size has private access in ArrayList
- How to write a test class to test my code?
- How to convert a String into an ArrayList?
- Java ArrayList of Doubles
- How to create a Multidimensional ArrayList in Java?
- Convert ArrayList
to java.util.List [closed] - In Java, what is a shallow copy?
- convert string to arraylist
in java - Good way to encapsulate Integer.parseInt()
- Eclipse IDE – Error: Build path specifies execution environment Java SE 1.7
- Java: Not a statement
- What is the difference between ArrayList.clear() and ArrayList.removeAll()?
- How to get the filename without the extension in Java?
- ShoppingCart.Java Program Assignment
- How Do I Implement an Insertion Sort Method for A Generic ArrayList?
- toring and Retrieving ArrayList values from hashmap
- return an ArrayList method
- How can I upgrade to Java 1.8 on an Amazon Linux Server?
- Exception in Application start method java.lang.reflect.InvocationTargetException
- Cannot find “Package Explorer” view in Eclipse
- Error: Could not find or load main class in intelliJ IDE
- Java Hashmap: How to get key from value?
- Pyspark: Exception: Java gateway process exited before sending the driver its port number
- Assign the negativeCntr with the number of negative values in the linked list
- Missing value auth-url required for auth plugin password
- JMS Topic vs Queues
- intellij incorrectly saying no beans of type found for autowired repository
- Warning: The method assertEquals from the type Assert is deprecated
- koch snowflake java recursion
- Java word count program
- Exception in thread “main” java.net.ConnectException: Connection refused: connect Socket Programming Java
- How can I read a text file in Android?
- Java says this method has a constructor name
- Test if element is present using Selenium WebDriver?
- Can't get on my own web page