There are two main types of arrays in Java. The classic static array has a size that is fixed at declaration. Once the memory space has been allocated, they can not be resized. The other type is known as ArrayLists and are a dynamically resizable construct available when the size of the array is not known in advance.
This topic assumes you have an understanding of arrays and object orientated programming
When instantiating an ArrayList, you can nominate the datatype to be contained by the array list or leave it unspecified (to allow for a potential mix of different data types). There is an example of each below.
ArrayLists require: import java.util.ArrayList;
Example 1: Create an ArrayList specifying elements to be String
ArrayList<String> alist = new ArrayList<String>();
alist.add("first");
alist.add("second");
for (String item: list) {
System.out.println( item );
}
Example 2: Create an ArrayList without specifying element type
ArrayList list = new ArrayList();
list.add( 3.14) ); // Add a float
list.add( true ); // Add a boolean
list.add( "Mixed data arrayList" ); // Add a string
list.add( 99 ); // Add an integer
for (Object o : list) {
if (o.getClass() == String.class) {
System.out.println("The item is a string and it's value was "+o);
} else if (o.getClass() == Integer.class) {
System.out.println("The item is an integer and it's value was "+o);
} else if (o.getClass() == Float.class) {
System.out.println("The item is a float and it's value was "+o);
} else if (o.getClass() == Boolean.class) {
System.out.println("The item is a boolean and it's value was "+o);
}
}
list.add( o )
- Add object o to the end of the listlist.add( i, o )
- Add object o at position index ilist.get( i )
- Returns the object at position index ilist.push( o )
- Add object o to the end of the listlist.pop()
- Returns the last item on the list and removes itlist.remove( i )
- Remove object at index position ilist.size()
- The number of items in the listAs arrays and ArrayLists both have beneifts over the other, the chances are high you are going to want to convert from one to the other at some time or another.
Array to ArrayList
int[] a = {1,2,3,5,7,11,13,17,19,23};
ArrayList alist = new ArrayList<>(Arrays.asList(a))
ArrayList to Array
String[] a = (String[])alist.toArray(new String[alist.size()]);
// or
int[] a = (int[])alist.toArray(new int[alist.size()]);
For some introductory level questions, I recommend solving the problem sets on coding bat:
Be warned, a number of the questions in the main problem set are quite challenging for new programmers. Do not worry if you feel some of them are beyond you if you are at the beginning stags of learning to program. Revisit the questions you can’t do after you have been programming consistently for about 12 months.
Note: Use ArrayLists as you feel best fits the problem. Do ensure you practice using a mix of both.
['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
Expected Output : ['Green', 'White', 'Black']
For question 8, you may like to copy and paste the following as an array to use:
String[] deck = {
"A♥️", "2♥️", "3♥️", "4♥️", "5♥️", "6♥️", "7♥️", "8♥️", "9♥️", "10♥️", "J♥️", "Q♥️", "K♥️",
"A♥️", "2♠️️", "3♠️", "4♠️", "5♠️", "6♠️", "7♠️", "8♠️", "9♠️", "10♠️", "J♠️", "Q♠️", "K♠️",
"A♦️", "2♦️", "3♦️", "4♦️", "5♦️", "6♦️", "7♦️", "8♦️", "9♦️", "10♦️", "J♦️", "Q♦️", "K♦️",
"A♣️", "2♣️", "3♣️", "4♣️", "5♣️", "6♣️", "7♣️", "8♣️", "9♣️", "10♣️", "J♣️", "Q♣️", "K♣️",
};