Static ArrayList in android
Ngày đăng:
02/01/2022
Trả lời:
0
Lượt xem:
66
In the last post we discussed about class ArrayList in Java and its important methods. Here we are sharing multiple ways to initialize an ArrayList with examples. Show Method 1: Initialization using Arrays.asListSyntax: ArrayListExample: Output: Elements are:[Pratap, Peter, Harsh]Method 2: Anonymous inner class method to initialize ArrayListSyntax: ArrayListExample: Output: Content of Array list cities:[Delhi, Agra, Chennai]Method3: Normal way of ArrayList initializationSyntax: ArrayListExample: Output: Books stored in array list are: [Java Book1, Java Book2, Java Book3]Method 4: Use Collections.ncopiesCollections.ncopies method can be used when we need to initialize the ArrayList with the same value for all of its elements. Syntax:count is number of elements and element is the item value ArrayListExample: import java.util.*; public class Details { public static void main(String args[]) { ArrayListOutput: ArrayList items: [5, 5, 5, 5, 5, 5, 5, 5, 5, 5] |