Home >Java >javaTutorial >How Can I Expand an Array in Java?
Expanding the Bounded Array
Arrays in Java have a fixed size that cannot be modified. However, there are situations where you may want to add additional elements to an existing array.
Ineffective Attempts
The code provided, using append(), will not work because arrays don't have this method. Arrays are immutable, meaning their size and elements cannot be changed directly.
Solution: Utilizing an ArrayList
A more flexible option is to use an ArrayList, a dynamically sized container that can grow and shrink as needed. Here's how you would accomplish the same functionality with an ArrayList:
List<String> where = new ArrayList<>(); where.add(ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1"); where.add(ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1");
If you later need to convert the ArrayList back to an array, you can use the toArray() method:
String[] simpleArray = where.toArray(new String[where.size()]);
Advantages of ArrayList
ArrayLists offer several advantages over arrays:
By leveraging ArrayLists, you can effectively handle arrays that need to be modified or expanded.
The above is the detailed content of How Can I Expand an Array in Java?. For more information, please follow other related articles on the PHP Chinese website!