Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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;
public class example{
    public static void main(String[]args){
        Scanner scan=new Scanner(System.in);
        int[] array=new int[6];
        for (int i=0;i<6;i++)
        array[i]=scan.nextInt();
        //First task
        System.out.println("The order you entered them: ");
        for (int i=0;i<6;i++)
        System.out.println(array[i]);
        //Second task
        System.out.println("The reversed order: ");
        for(int i=5;i>=0;i--)
        System.out.println(array[i]);
    }
}
I used Scanner class here instead of Keyboard class that I used often.

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
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
Visit this user's website Find all posts by this user
Quote this message in a reply
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.

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
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.
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;
public class example{
    public static void main(String[]args){
        Scanner scan=new Scanner(System.in);
        int[] array=new int[6];
        for (int i=0;i<6;i++)
        array[i]=scan.nextInt();
        //First task
        System.out.println("The order you entered them: ");
        for (int i=0;i<6;i++)
        System.out.println(array[i]);
        //Second task
        System.out.println("The reversed order: ");
        for(int i=5;i>=0;i--)
        System.out.println(array[i]);
    }
}
I used Scanner class here instead of Keyboard class that I used often.

You can make anything array even classes Wink not just variables Tongue
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: