|
The basics of arrays in Java
|
|
12-08-2009, 03:09 AM
(This post was last modified: 12-08-2009 03:31 AM by Alphablend.)
Post: #1
|
|||
|
|||
|
The basics of arrays in Java
These are the basics of arrays in Java.
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. Declaring: <type>[] <name of array>=new <type>[<length of array>]; Example for integer & string: int[] array1=new int[10]; String[] array2=new String[6]; You can declare arrays of other types: byte[] anArrayOfBytes; short[] anArrayOfShorts; long[] anArrayOfLongs; float[] anArrayOfFloats; double[] anArrayOfDoubles; boolean[] anArrayOfBooleans; char[] anArrayOfChars; String[] anArrayOfStrings; Example of one array: array1= 2 4 6 8 10 index 0 1 2 3 4 Each item in an array is called an element, and each element is accessed by its numerical index. Numbering begins with 0. You can access a single element of array like this: array1[2] <== this value is 6 You can normally change a value of any element of the array: array1[2]=12 I will take the most simple example of all. In the program I will define an array which has 6 elements of type integer (his length is 6). You enter those elements from keyboard and: a. you just print them on the screen. b. you print them on screen but backwards from the order you entered them. Code: import java.util.Scanner;![]()
|
|||
|
12-08-2009, 03:11 AM
Post: #2
|
|||
|
|||
|
RE: The basics of arrays in Java
Very good explanation, but i have 2 negative critics :
-You should make it longer and more explained -Those are not basics of arrays, those are basics of one-dimensional arrays ! There's a fine line between genius and insanity. I have erased this line. Oscar Levant There's a fine line between an administrator and black hat hacker. I have erased this line. Dr DEBCOL |
|||
|
12-08-2009, 03:34 AM
(This post was last modified: 12-08-2009 03:35 AM by Alphablend.)
Post: #3
|
|||
|
|||
|
RE: The basics of arrays in Java
These were just the basics that I've learnt.
This updated version contains some info from http://java.sun.com/. Click HERE to visit the link to arrays on that page. ![]()
|
|||
|
12-08-2009, 04:06 AM
Post: #4
|
|||
|
|||
RE: The basics of arrays in Java
(12-08-2009 03:09 AM)Alphablend Wrote: These are the basics of arrays in Java. You can make anything array even classes not just variables
|
|||
|
« Next Oldest | Next Newest »
|




![[Image: 45669_pythonlogo.png]](http://myph.us/pics/45669_pythonlogo.png)



not just variables