Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics) An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the "Hello World! " application. This section discusses arrays in greater detail. An array of 10 elements. Each item in an array is called an element, and each element is accessed by its numerical index. The following program, ArrayDemo, creates an array of integers, puts some values in the array, and prints each value to standard output. The output from this program is: Element at index 0: 100 Element at index 1: 200 Element at index 2: 300 Element at index 3: 400 Element at index 4: 500 Element at index 5: 600 Element at index 6: 700 Element at index 7: 800 Element at index 8: 900 Element at index 9: 1000 Declaring a Variable to Refer to an Array The preceding program declares an array (named anArray) with the following line of code:
Pjav1408 Array prov. Trial: array. Prov Array och objekt pjav1408. Collection (Java 2 Platform SE v1.4.2) Lesson: Introduction to Collections (The Java™ Tutorials > Collections) A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers). If you have used the Java programming language — or just about any other programming language — you are already familiar with collections. What Is a Collections Framework? A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following: Interfaces: These are abstract data types that represent collections.
Apart from the Java Collections Framework, the best-known examples of collections frameworks are the C++ Standard Template Library (STL) and Smalltalk's collection hierarchy. Introduction to Arrays in Java (Java Programming Tutorial. Contextual Ads More Java Resources Java Tutorials Java Code Java Jobs Advertisement Creating an Array There are two kinds of data in Java: primitive types (such as int and double) and objects. Int[] intArray; // defines a reference to an array intArray = new int[100]; // creates the array, and sets intArray to refer to it Or you can use the equivalent single-statement approach: int[] intArray = new int[100]; The [] operator is the sign to the compiler we’re naming an array object and not an ordinary variable. Int intArray[] = new int[100]; // alternative syntax However, placing the [] after the int makes it clear that the [] is part of the type, not the name.
Because an array is an object, its name - in the preceding code - is a reference to an array; it’s not the array itself. Arrays have a length field, which you can use to find the size (the number of elements) of an array: int arrayLength = intArray.length; // find array size Accessing Array Elements Initialization An Array Example // array.java.