Mục lục bài viết
Update: 2021-12-14 01:01:06,Bạn Cần biết về Can you make an ArrayList of ints?. Bạn trọn vẹn có thể lại Comment ở cuối bài để Ad được tương hỗ.
The ArrayList class is a Java class that you can use to store lists of objects. You can also store objects in an array, but arrays have a couple of obvious problems.
Tóm lược đại ý quan trọng trong bài
The primary goal of the Java ArrayList class is to provide a class that replicates many of the features of arrays, while adding some new features that are designed to work around the problems listed above.
To create an ArrayList object you use the following syntax.
ArrayList A = new ArrayList();
Here Type is the type that you are planning to store in the ArrayList. For example, to make an ArrayList that can hold Strings you would do
ArrayList B = new ArrayList();
A fundamental limitation of ArrayLists is that they can only hold objects, and not primitive types such as ints.
ArrayList C = new ArrayList();
// Illegal – int is not an object type
The workaround for this is that Java provides a class equivalent for every one of the primitive types. For example, there is an Integer class corresponding to the int type and a Double class corresponding to the double type, and so on. The first step to being able to store a list of ints in an ArrayList is to instead create an ArrayList that can hold a list of Integer objects:
ArrayList D = new ArrayList();
// OK – Integer is an object type
When you create an ArrayList, the list you get is initially empty. To add new items to the end of the ArrayList you use the add() method:
ArrayList B = new ArrayList(); // B starts out empty.
B.add(“Hello”); // B now has size 1
B.add(“World”); // B new has size 2
Adding items to an ArrayList of Integers is similar:
ArrayList D = new ArrayList();
D.add(new Integer(3)); // D now has size 1
D.add(new Integer(15)); // D now has size 2
An alternative way to add ints to an ArrayList is to use the autoboxing feature. This feature automatically converts primitive types into their object equivalents:
ArrayList D = new ArrayList();
D.add(3); // D now has size 1
D.add(15); // D now has size 2
To retrieve items from an ArrayList, use the get() method.
ArrayList D = new ArrayList();
D.add(3);
D.add(15);
int x = D.get(1); // x is now 15
To determine the size of an ArrayList, use the size() method.
ArrayList D = new ArrayList();
D.add(3);
D.add(15);
D.add(-46);
for(int n = 0;n < D.size();n++)
System.out.println(D.get(n));
To replace an existing value with a new value, use the set() method:
ArrayList D = new ArrayList();
D.add(3); // Location 0 is 3
D.add(15);
D.add(-46);
D.set(0,22); // Location 0 is now 22
Along with being to add items and have the list resize automatically to accomodate them, you can also remove items and have the list shrink automatically each time you remove an item. To remove an item, use the remove() method with the index of the item you want to remove. For example, to remove the last item in an ArrayList you would do
D.remove(D.size() – 1);
– Một số Keyword tìm kiếm nhiều : ” đoạn Clip hướng dẫn Can you make an ArrayList of ints? tiên tiến và phát triển nhất , Chia Sẻ Link Cập nhật Can you make an ArrayList of ints? “.
You trọn vẹn có thể để lại phản hồi nếu gặp yếu tố chưa hiểu nghen.
#ArrayList #ints