Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- public class ArrayListToArray {
- public static void main(String[] args) {
- List<String> animals = new ArrayList<>();
- animals.add("bird");
- animals.add("dog");
- animals.add("fish");
- animals.add("cat");
- String [] newAnimalsArray = animals.toArray(new String[0]);
- System.out.println(newAnimalsArray.length);
- //Creating a new String array. If you specify a size of 0 for the parameter, Java will create a new array of the proper size for the return value.
- String [] newAnimalsArray = animals.toArray(new String[9]);
- System.out.println(newAnimalsArray.length);
- //If you specify a length that the ArrayList already fits in (i.e. Array longer than its ArrayList) it will be returned. Otherwise, a new one will be created.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement