Arrays
- 100% developed as of Dec 31, 2012 Statements
- 100% developed as of Mar 10, 2013 Conditional blocks
- 100% developed as of Mar 10, 2013 Loop blocks
- 100% developed as of May 24, 2013 Boolean expressions
- 100% developed as of Feb 16, 2010 Variables
- 100% developed as of Mar 10, 2013 Primitive Types
- 100% developed as of Mar 10, 2013 Arithmetic expressions
- 100% developed as of May 24, 2013 Literals
- 100% developed as of Mar 10, 2013 Methods
- 100% developed as of May 24, 2013 String
- 100% developed as of Mar 10, 2013 Objects
- 100% developed as of Jul 5, 2012 Packages
- 100% developed as of Mar 10, 2013 Arrays
- 75% developed as of Jan 11, 2013 Mathematical functions
- 75% developed as of Jan 11, 2013 Large numbers
- 75% developed as of Jan 11, 2013 Random numbers
- 100% developed as of Apr 8, 2013 Unicode
- 100% developed as of Apr 8, 2013 Comments
- 100% developed as of Sep 27, 2007 Keywords
- 100% developed as of Aug 6, 2013 Coding conventions
- 0% developed as of Mar 26, 2018 Lambda expressions
An array is similar to a table of objects or primitive types, keyed by index. You may have noticed the strange parameter of the default main()
method (String[] args
) since the beginning of the book. It is an array. Let's handle this parameter:
publicclass ArrayExample{ publicstaticvoidmain(String[]args){ for(inti=0;i<args.length;++i){ System.out.println("Argument #"+(i+1)+": "+args[i]); } } }
$ java ArrayExample This is a test Argument #1 This Argument #2 is Argument #3 a Argument #4 test
In the code listing 3.15, the array is args
. It is an array of String
objects (here those objects are the words that have been typed by the user at the program launching). At line 4, One contained object is accessed using its index in the array. You can see that its value is printed on the standard output. Note that the strings have been put in the array with the right order.
Fundamentals
[edit | edit source ]In Java, an array is an object. This object has a given type for the contained primitive types or objects (int
, char
, String
, ...). An array can be declared in several ways:
int[]array1=null; intarray2[]=null;
Those syntaxes are identical but the first one is recommended. It can also be instantiated in several ways:
array1=newint[10]; int[]array0={1,2,3,4,5,6,7,8,9,10};//this only works in the declaration array1=newint[]{1,2,3,4,5,6,7,8,9,10};
At line 1, we instantiate an array of 10 items that get the default value (which is 0 for int
). At lines 2 and 3, we instantiate arrays of 10 given items. It will each be given an index according to its order. We can know the size of the array using the length
attribute:
intnbItems=10; Object[]array3=newObject[nbItems]; System.out.println(array3.length);
10
Arrays are allocated at runtime, so the specified size in an array creation expression may be a variable (rather than a constant expression as in C). However, the size of an instantiated array never changes. If you need to change the size, you have to create a new instance. Items can be accessed by their index. Beware! The first index is 0:
char[]array4={'a','b','c','d','e'}; System.out.println(array4[2]); array4[4]='z'; System.out.println(array4[4]);
c z
If you attempt to access to a too high index or negative index, you will get an ArrayIndexOutOfBoundsException
.
Question 3.20: Consider the following code:
publicclass Question20{ publicstaticvoidmain(String[]args){ String[]listOfWord={"beggars","can't","be","choosers"}; System.out.println(listOfWord[1]); System.out.println(listOfWord[listOfWord.length-1]); } }
What will be printed in the standard output?
can't choosers
Indexes start at 0. So the index 1 point at the second string (can't
). There are 4 items so the size of the array is 4. Hence the item pointed by the index 3 is the last one (choosers
).
Two-Dimensional Arrays
[edit | edit source ]Actually, there are no two-dimensional arrays in Java. However, an array can contain any class of object, including an array:
String[][]twoDimArray={{"a","b","c","d","e"}, {"f","g","h","i","j"}, {"k","l","m","n","o"}}; int[][]twoDimIntArray={{0,1,2,3,4}, {10,11,12,13,14}, {20,21,22,23,24}};
It's not exactly equivalent to two-dimensional arrays because the size of the sub-arrays may vary. The sub-array reference can even be null. Consider:
String[][]weirdTwoDimArray={{"10","11","12"}, null, {"20","21","22","23","24"}};
Note that the length of a two-dimensional array is the number of one-dimensional arrays it contains. In the above example, weirdTwoDimArray.length
is 3, whereas weirdTwoDimArray[2].length
is 5.
In the code section 3.58, we defined an array that has three elements, each element contains an array having 5 elements. We could create the array having the 5 elements first and use that one in the initialize block.
String[]oneDimArray={"00","01","02","03","04"}; String[][]twoDimArray={oneDimArray, {"10","11","12","13","14"}, {"20","21","22","23","24"}};
Question 3.21: Consider the following code:
String[][]alphabet={{"a","b","c","d","e"}, {"f","g","h","i","j"}, {"k","l","m","n","o"}, {"p","q","r","s","t"}, {"u","v","w","x","y"}, {"z"}};
Print the whole alphabet in the standard output.
publicclass Answer21{ publicstaticvoidmain(String[]args){ String[][]alphabet={{"a","b","c","d","e"}, {"f","g","h","i","j"}, {"k","l","m","n","o"}, {"p","q","r","s","t"}, {"u","v","w","x","y"}, {"z"}}; for(inti=0;i<alphabet.length;i++){ for(intj=0;j<alphabet[i].length;j++){ System.out.println(alphabet[i][j]); } } } }
i
will be the indexes of the main array and j
will be the indexes of all the sub-arrays. We have to first iterate on the main array. We have to read the size of the array. Then we iterate on each sub-array. We have to read the size of each array as it may vary. Doing so, we iterate on all the sub-array items using the indexes. All the items will be read in the right order.
Multidimensional Array
[edit | edit source ]Going further any number of dimensional array can be defined.
or