Sie sind auf Seite 1von 22

Java Technical Training

Week 2: Arrays
An array is a collection of variables of the same type. It stores a fixed size sequential collection of
elements of the same type. An array is used to store a collection of data.

Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any
type only that variable is of type array remaining variables in that declaration are not array variables
those are normal variables of that type. If you put [] (square brackets) after any data type all the
variables in that declaration are array variables. All the elements in the array are accessed with index.
The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting
index is 0 and ending index is 4. In this tutorial you can learn how to declare arrays, how to assign
values to arrays and how get values from arrays.

Syntax:
<data type> <variable>[];
<data type>[] <variable>;

boolean Array

It is used to store boolean data type values only. With the following examples you can learn how to
declare boolean array, how to assign values to boolean array and how to get values from boolean array.

/* boolean Array Example */


/* Save with file name BooleanArray.java */

public class BooleanArray


{
public static void main(String args[])
{
//BOOLEAN ARRAY DECLARATION
boolean b[];
//MEMORY ALLOCATION FOR BOOLEAN ARRAY
b = new boolean[4];
//ASSIGNING ELEMENTS TO BOOLEAN ARRAY
b[0] = true;
b[1] = false;
b[2] = false;
b[3] = false;
//BOOLEAN ARRAY OUTPUT
System.out.println();
System.out.println("boolean Array Example");
System.out.println("=====================");
System.out.println();
for(int i=0;i<b.length;i++)
{
System.out.println("Element at Index : "+ i + " " + b[i]);
Java Technical Training
}
}
}

In this example you can learn how to assign values to boolean array at the time of declaration.

/* boolean Array Example 2 */


/* Save with file name BooleanArray2.java */

public class BooleanArray2


{
public static void main(String args[])
{
//BOOLEAN ARRAY DECLARATION AND ASSIGNMENT
boolean b[] = {false,false,true,true};
//BOOLEAN ARRAY OUTPUT
System.out.println();
System.out.println("boolean Array Example");
System.out.println("=====================");
System.out.println();
for(int i=0;i<b.length;i++)
{
System.out.println("Element at Index : "+ i + " " + b[i]);
}
}
}

In this example you can learn how to declare boolean array with other boolean variables.

/* boolean Array Example 3 */


/* Save with file name BooleanArray3.java */

public class BooleanArray3


{
public static void main(String args[])
{
//BOOLEAN ARRAY DECLARATION WITH OTHER NORMAL VARIABLES
boolean b[], a; //b IS AN ARRAY a IS NOT AN ARRAY
//ASSIGN VALUES TO BOOLEAN ARRAY
b = new boolean[5];
b[0] = true;
b[1] = false;
b[2] = false;
b[3] = false;
a = true;
//BOOLEAN ARRAY OUTPUT
System.out.println();
System.out.println("boolean Array Example");
Java Technical Training
System.out.println("=====================");
System.out.println();
System.out.println("a value is : "+a);
System.out.println();
for(int i=0;i<b.length;i++)
{
System.out.println("Element at Index : "+ i + " " + b[i]);
}
}
}

In this example you can learn how to assign boolean array to other boolean array.

/* boolean Array Example 4 */


/* Save with file name BooleanArray4.java */

public class BooleanArray4


{
public static void main(String args[])
{
//BOOLEAN ARRAYS DECLARATION
boolean[] a, b; //a AND b ARE ARRAY VARIABLES
//ASSIGN VALUES TO BOOLEAN ARRAY
b = new boolean[5];
b[0] = true;
b[1] = false;
b[2] = false;
b[3] = false;
//ASSIGNING b ARRAY TO a ARRAY VARIABLE
a = b;
//BOOLEAN ARRAY OUTPUT
System.out.println();
System.out.println("boolean Array Example");
System.out.println("=====================");
System.out.println();
System.out.println("b array values");
for(int i=0;i<b.length;i++)
{
System.out.println("Element at Index : "+ i + " " + b[i]);
}
System.out.println();
System.out.println("a array values");
for(int i=0;i<b.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
}
}
Java Technical Training

byte Array

It is used to store byte data type values only. With the following examples you can learn how to declare
byte array, how to assign values to byte array and how to get values from byte array.

/* byte Array Example */


/* Save with file name ByteArray.java */

public class ByteArray


{
public static void main(String args[])
{
//BYTE ARRAY DECLARATION
byte b[];
//MEMORY ALLOCATION FOR BYTE ARRAY
b = new byte[4];
//ASSIGNING ELEMENTS TO BYTE ARRAY
b[0] = 20;
b[1] = 10;
b[2] = 30;
b[3] = 5;
//BYTE ARRAY OUTPUT
System.out.println();
System.out.println("byte Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<b.length;i++)
{
System.out.println("Element at Index : "+ i + " " + b[i]);
}
}
}

In this example you can learn how to assign values to byte array at the time of declaration.

/* byte Array Example 2 */


/* Save with file name ByteArray2.java */

public class ByteArray2


{
public static void main(String args[])
{
//BYTE ARRAY DECLARATION AND ASSIGNMENT
byte b[] = {20,10,30,5};
//BYTE ARRAY OUTPUT
System.out.println();
Java Technical Training
System.out.println("byte Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<b.length;i++)
{
System.out.println("Element at Index : "+ i + " " + b[i]);
}
}
}

In this example you can learn how to declare byte array with other byte variables.

/* byte Array Example 3 */


/* Save with file name ByteArray3.java */

public class ByteArray3


{
public static void main(String args[])
{
//BYTE ARRAY DECLARATION
byte b[], a; //b IS AN ARRAY a IS NOT AN ARRAY
//MEMORY ALLOCATION FOR BYTE ARRAY
b = new byte[4];
//ASSIGNING ELEMENTS TO BYTE ARRAY
b[0] = 20;
b[1] = 10;
b[2] = 30;
b[3] = 5;
a = 100;
//BYTE ARRAY OUTPUT
System.out.println();
System.out.println("byte Array Example");
System.out.println("==================");
System.out.println();
System.out.println("a value is : "+a);
System.out.println();
for(int i=0;i<b.length;i++)
{
System.out.println("Element at Index : "+ i + " " + b[i]);
}
}
}

In this example you can learn how to assign byte array to other byte array.

/* byte Array Example 4 */


/* Save with file name ByteArray4.java */
Java Technical Training
public class ByteArray4
{
public static void main(String args[])
{
//BYTE ARRAY DECLARATION
byte[] a, b; //a AND b ARE ARRAY VARIABLES
//MEMORY ALLOCATION FOR BYTE ARRAY
b = new byte[4];
//ASSIGNING ELEMENTS TO BYTE ARRAY
b[0] = 20;
b[1] = 10;
b[2] = 30;
b[3] = 5;
//ASSIGNING b ARRAY TO a ARRAY VARIABLE
a = b;
//BYTE ARRAY OUTPUT
System.out.println();
System.out.println("byte Array Example");
System.out.println("==================");
System.out.println();
System.out.println("b array values");
for(int i=0;i<b.length;i++)
{
System.out.println("Element at Index : "+ i + " " + b[i]);
}
System.out.println();
System.out.println("a array values");
for(int i=0;i<b.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
}
}

char Array

It is used to store char data type values only. With the following examples you can learn how to declare
char array, how to assign values to char array and how to get values from char array.

/* char Array Example */


/* Save with file name CharArray.java */

public class CharArray


{
public static void main(String args[])
{
//CHAR ARRAY DECLARATION
char c[];
Java Technical Training
//MEMORY ALLOCATION FOR CHAR ARRAY
c = new char[4];
//ASSIGNING ELEMENTS TO CHAR ARRAY
c[0] = 'a';
c[1] = 'c';
c[2] = 'D';
c[3] = 'B';
//CHAR ARRAY OUTPUT
System.out.println();
System.out.println("char Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<c.length;i++)
{
System.out.println("Element at Index : "+ i + " " + c[i]);
}
}
}

In this example you can learn how to assign values to char array at the time of declaration.

/* char Array Example 2 */


/* Save with file name CharArray2.java */

public class CharArray2


{
public static void main(String args[])
{
//CHAR ARRAY DECLARATION AND ASSIGNMENT
char c[] = {'a', 'c', 'D', 'B'};
//CHAR ARRAY OUTPUT
System.out.println();
System.out.println("char Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<c.length;i++)
{
System.out.println("Element at Index : "+ i + " " + c[i]);
}
}
}

In this example you can learn how to declare char array with other char variables.

/* char Array Example 3 */


/* Save with file name CharArray3.java */

public class CharArray3


Java Technical Training
{
public static void main(String args[])
{
//CHAR ARRAY DECLARATION
char c[], a;//c IS AN ARRAY a IS NOT AN ARRAY
//MEMORY ALLOCATION FOR CHAR ARRAY
c = new char[4];
//ASSIGNING ELEMENTS TO CHAR ARRAY
c[0] = 'a';
c[1] = 'c';
c[2] = 'D';
c[3] = 'B';
a = 'X';
//CHAR ARRAY OUTPUT
System.out.println();
System.out.println("char Array Example");
System.out.println("==================");
System.out.println();
System.out.println("a value is : "+a);
System.out.println();
for(int i=0;i<c.length;i++)
{
System.out.println("Element at Index : "+ i + " " + c[i]);
}
}
}

In this example you can learn how to assign char array to other char array.

/* char Array Example 4 */


/* Save with file name CharArray4.java */

public class CharArray4


{
public static void main(String args[])
{
//CHAR ARRAY DECLARATION
char[] c, a;//c AND a ARE ARRAY VARIABLES
//MEMORY ALLOCATION FOR CHAR ARRAY
c = new char[4];
//ASSIGNING ELEMENTS TO CHAR ARRAY
c[0] = 'a';
c[1] = 'c';
c[2] = 'D';
c[3] = 'B';
//ASSIGNING c ARRAY TO a ARRAY VARIABLE
a = c;
//CHAR ARRAY OUTPUT
Java Technical Training
System.out.println();
System.out.println("char Array Example");
System.out.println("==================");
System.out.println();
System.out.println("c array values");
for(int i=0;i<c.length;i++)
{
System.out.println("Element at Index : "+ i + " " + c[i]);
}
System.out.println();
System.out.println("a array values");
for(int i=0;i<a.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
}
}

short Array

It is used to store short data type values only. With the following examples you can learn how to declare
short array, how to assign values to short array and how to get values from short array.

/* short Array Example */


/* Save with file name ShortArray.java */

public class ShortArray


{
public static void main(String args[])
{
//SHORT ARRAY DECLARATION
short s[];
//MEMORY ALLOCATION FOR SHORT ARRAY
s = new short[4];
//ASSIGNING ELEMENTS TO SHORT ARRAY
s[0] = 10;
s[1] = 30;
s[2] = 40;
s[3] = 7;
//SHORT ARRAY OUTPUT
System.out.println();
System.out.println("short Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<s.length;i++)
{
System.out.println("Element at Index : "+ i + " " + s[i]);
}
Java Technical Training
}
}

In this example you can learn how to assign values to short array at the time of declaration.

/* short Array Example 2 */


/* Save with file name ShortArray2.java */

public class ShortArray2


{
public static void main(String args[])
{
//SHORT ARRAY DECLARATION AND ASSIGNMENT
short s[] = {10,30,40,7};
//SHORT ARRAY OUTPUT
System.out.println();
System.out.println("short Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<s.length;i++)
{
System.out.println("Element at Index : "+ i + " " + s[i]);
}
}
}

In this example you can learn how to declare short array with other short variables.

/* short Array Example 3 */


/* Save with file name ShortArray3.java */

public class ShortArray3


{
public static void main(String args[])
{
//SHORT ARRAY DECLARATION
short s[], s2; //s IS AN ARRAY s2 IS NOT AN ARRAY
//MEMORY ALLOCATION FOR SHORT ARRAY
s = new short[4];
//ASSIGNING ELEMENTS TO SHORT ARRAY
s[0] = 10;
s[1] = 30;
s[2] = 40;
s[3] = 7;
s2 = 101;
//SHORT ARRAY OUTPUT
System.out.println();
System.out.println("short Array Example");
Java Technical Training
System.out.println("==================");
System.out.println();
System.out.println("value of s2 : "+s2);
System.out.println();
for(int i=0;i<s.length;i++)
{
System.out.println("Element at Index : "+ i + " " + s[i]);
}
}
}

In this example you can learn how to assign short array to other short array.

/* short Array Example 4 */


/* Save with file name ShortArray4.java */

public class ShortArray4


{
public static void main(String args[])
{
//SHORT ARRAY DECLARATION
short[] s, s2; //s AND s2 ARE ARRAY VARIABLES
//MEMORY ALLOCATION FOR SHORT ARRAY
s = new short[4];
//ASSIGNING ELEMENTS TO SHORT ARRAY
s[0] = 10;
s[1] = 30;
s[2] = 40;
s[3] = 7;
//ASSIGNING s ARRAY TO s2 ARRAY VARIABLE
s2 = s;
//SHORT ARRAY OUTPUT
System.out.println();
System.out.println("short Array Example");
System.out.println("==================");
System.out.println();
System.out.println("s array values");
for(int i=0;i<s.length;i++)
{
System.out.println("Element at Index : "+ i + " " + s[i]);
}
System.out.println();
System.out.println("s2 array values");
for(int i=0;i<s2.length;i++)
{
System.out.println("Element at Index : "+ i + " " + s2[i]);
}
}
Java Technical Training
}

int Array

It is used to store int data type values only. With the following examples you can learn how to declare
int array, how to assign values to int array and how to get values from int array.

/* int Array Example */


/* Save with file name IntArray.java */

public class IntArray


{
public static void main(String args[])
{
//INT ARRAY DECLARATION
int n[];
//MEMORY ALLOCATION FOR INT ARRAY
n = new int[4];
//ASSIGNING ELEMENTS TO INT ARRAY
n[0] = 5;
n[1] = 3;
n[2] = 4;
n[3] = 7;
//INT ARRAY OUTPUT
System.out.println();
System.out.println("int Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<n.length;i++)
{
System.out.println("Element at Index : "+ i + " " + n[i]);
}
}
}

In this example you can learn how to assign values to int array at the time of declaration.

/* int Array Example 2 */


/* Save with file name IntArray2.java */

public class IntArray2


{
public static void main(String args[])
{
//INT ARRAY DECLARATION
int n[] = {5,3,4,7};
//INT ARRAY OUTPUT
Java Technical Training
System.out.println();
System.out.println("int Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<n.length;i++)
{
System.out.println("Element at Index : "+ i + " " + n[i]);
}
}
}

In this example you can learn how to declare int array with other int variables.

/* int Array Example 3 */


/* Save with file name IntArray3.java */

public class IntArray3


{
public static void main(String args[])
{
//INT ARRAY DECLARATION
int n[], n2; //n IS AN ARRAY n2 IS NOT AN ARRAY
//MEMORY ALLOCATION FOR INT ARRAY
n = new int[4];
//ASSIGNING ELEMENTS TO INT ARRAY
n[0] = 5;
n[1] = 3;
n[2] = 4;
n[3] = 7;
n2 = 555;
//INT ARRAY OUTPUT
System.out.println();
System.out.println("int Array Example");
System.out.println("==================");
System.out.println();
System.out.println("n2 value is : "+n2);
System.out.println();
for(int i=0;i<n.length;i++)
{
System.out.println("Element at Index : "+ i + " " + n[i]);
}
}
}

In this example you can learn how to assign int array to other int array.

/* int Array Example 4 */


/* Save with file name IntArray4.java */
Java Technical Training

public class IntArray4


{
public static void main(String args[])
{
//INT ARRAY DECLARATION
int[] n, n2; //n AND n2 ARE ARRAY VARIABLES
//MEMORY ALLOCATION FOR INT ARRAY
n = new int[4];
//ASSIGNING ELEMENTS TO INT ARRAY
n[0] = 5;
n[1] = 3;
n[2] = 4;
n[3] = 7;
//ASSIGNING n ARRAY TO n2 ARRAY VARIABLE
n2 = n;
//INT ARRAY OUTPUT
System.out.println();
System.out.println("int Array Example");
System.out.println("==================");
System.out.println();
System.out.println("n array values");
System.out.println();
for(int i=0;i<n.length;i++)
{
System.out.println("Element at Index : "+ i + " " + n[i]);
}
System.out.println();
System.out.println("n2 array values");
for(int i=0;i<n2.length;i++)
{
System.out.println("Element at Index : "+ i + " " + n2[i]);
}
}
}

float Array

It is used to store float data type values only. With the following examples you can learn how to declare
float array, how to assign values to float array and how to get values from float array.

/* float Array Example */


/* Save with file name FloatArray.java */

public class FloatArray


{
public static void main(String args[])
Java Technical Training
{
//FLOAT ARRAY DECLARATION
float f[];
//MEMORY ALLOCATION FOR FLOAT ARRAY
f = new float[4];
//ASSIGNING ELEMENTS TO FLOAT ARRAY
f[0] = 10.10f;
f[1] = 30.3f;
f[2] = 40.60f;
f[3] = 77.50f;
//FLOAT ARRAY OUTPUT
System.out.println();
System.out.println("float Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<f.length;i++)
{
System.out.println("Element at Index : "+ i + " " + f[i]);
}
}
}

In this example you can learn how to assign values to float array at the time of declaration.

/* float Array Example 2 */


/* Save with file name FloatArray2.java */

public class FloatArray2


{
public static void main(String args[])
{
//FLOAT ARRAY DECLARATION
float f[] = {10.10f,30.3f,40.60f,77.50f};
//FLOAT ARRAY OUTPUT
System.out.println();
System.out.println("float Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<f.length;i++)
{
System.out.println("Element at Index : "+ i + " " + f[i]);
}
}
}

In this example you can learn how to declare float array with other float variables.

/* float Array Example 3 */


Java Technical Training
/* Save with file name FloatArray3.java */

public class FloatArray3


{
public static void main(String args[])
{
//FLOAT ARRAY DECLARATION
float f[], f2; //f IS AN ARRAY f2 IS NOT AN ARRAY
//MEMORY ALLOCATION FOR FLOAT ARRAY
f = new float[4];
//ASSIGNING ELEMENTS TO FLOAT ARRAY
f[0] = 10.10f;
f[1] = 30.3f;
f[2] = 40.60f;
f[3] = 77.50f;
f2 = 555.55f;
//FLOAT ARRAY OUTPUT
System.out.println();
System.out.println("float Array Example");
System.out.println("==================");
System.out.println();
System.out.println("f2 value is : "+f2);
System.out.println();
for(int i=0;i<f.length;i++)
{
System.out.println("Element at Index : "+ i + " " + f[i]);
}
}
}

In this example you can learn how to assign float array to other float array.

/* float Array Example 4 */


/* Save with file name FloatArray4.java */

public class FloatArray4


{
public static void main(String args[])
{
//FLOAT ARRAY DECLARATION
float[] f, f2; //f AND f2 ARE ARRAY VARIABLES
//MEMORY ALLOCATION FOR FLOAT ARRAY
f = new float[4];
//ASSIGNING ELEMENTS TO FLOAT ARRAY
f[0] = 10.10f;
f[1] = 30.3f;
f[2] = 40.60f;
f[3] = 77.50f;
Java Technical Training
//ASSIGNING f ARRAY TO f2 ARRAY VARIABLE
f2 = f;
//FLOAT ARRAY OUTPUT
System.out.println();
System.out.println("float Array Example");
System.out.println("==================");
System.out.println();
System.out.println("f array values");
System.out.println();
for(int i=0;i<f.length;i++)
{
System.out.println("Element at Index : "+ i + " " + f[i]);
}
System.out.println();
System.out.println("f2 array values");
for(int i=0;i<f2.length;i++)
{
System.out.println("Element at Index : "+ i + " " + f2[i]);
}
}
}

long Array

It is used to store long data type values only. With the following examples you can learn how to declare
long array, how to assign values to long array and how to get values from long array.

/* long Array Example */


/* Save with file name LongArray.java */

public class LongArray


{
public static void main(String args[])
{
//LONG ARRAY DECLARATION
long a[];
//MEMORY ALLOCATION FOR LONG ARRAY
a = new long[4];
//ASSIGNING ELEMENTS TO LONG ARRAY
a[0] = 100000L;
a[1] = 300000L;
a[2] = 400000L;
a[3] = 786777L;
//LONG ARRAY OUTPUT
System.out.println();
System.out.println("long Array Example");
System.out.println("==================");
System.out.println();
Java Technical Training
for(int i=0;i<a.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
}
}

In this example you can learn how to assign values to long array at the time of declaration.

/* long Array Example 2 */


/* Save with file name LongArray2.java */

public class LongArray2


{
public static void main(String args[])
{
//LONG ARRAY DECLARATION
long a[] = {100000L,300000L,400000L,786777L};
//LONG ARRAY OUTPUT
System.out.println();
System.out.println("long Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<a.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
}
}

In this example you can learn how to declare long array with other long variables.

/* long Array Example 3 */


/* Save with file name LongArray3.java */

public class LongArray3


{
public static void main(String args[])
{
//LONG ARRAY DECLARATION
long a[], a2; //a IS AN ARRAY a2 IS NOT AN ARRAY
//MEMORY ALLOCATION FOR LONG ARRAY
a = new long[4];
//ASSIGNING ELEMENTS TO LONG ARRAY
a[0] = 100000L;
a[1] = 300000L;
a[2] = 400000L;
a[3] = 786777L;
Java Technical Training
a2 = 222222L;
//LONG ARRAY OUTPUT
System.out.println();
System.out.println("long Array Example");
System.out.println("==================");
System.out.println();
System.out.println("a2 value is : "+a2);
System.out.println();
for(int i=0;i<a.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
}
}

In this example you can learn how to assign long array to other long array.

/* long Array Example 4 */


/* Save with file name LongArray4.java */

public class LongArray4


{
public static void main(String args[])
{
//LONG ARRAY DECLARATION
long[] a, a2; //a AND a2 ARE ARRAY VARIABLES
//MEMORY ALLOCATION FOR LONG ARRAY
a = new long[4];
//ASSIGNING ELEMENTS TO LONG ARRAY
a[0] = 100000L;
a[1] = 300000L;
a[2] = 400000L;
a[3] = 786777L;
//ASSIGNING a ARRAY TO a2 ARRAY VARIABLE
a2 = a;
//LONG ARRAY OUTPUT
System.out.println();
System.out.println("long Array Example");
System.out.println("==================");
System.out.println();
System.out.println("a array values");
System.out.println();
for(int i=0;i<a.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
System.out.println();
System.out.println("a2 array values");
Java Technical Training
System.out.println();
for(int i=0;i<a2.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a2[i]);
}
}
}

double Array

It is used to store double data type values only. With the following examples you can learn how to
declare double array, how to assign values to double array and how to get values from double array.

/* double Array Example */


/* Save with file name DoubleArray.java */

public class DoubleArray


{
public static void main(String args[])
{
//DOUBLE ARRAY DECLARATION
double a[];
//MEMORY ALLOCATION FOR DOUBLE ARRAY
a = new double[4];
//ASSIGNING ELEMENTS TO DOUBLE ARRAY
a[0] = 100000d;
a[1] = 300000d;
a[2] = 400000d;
a[3] = 786777d;
//DOUBLE ARRAY OUTPUT
System.out.println();
System.out.println("double Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<a.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
}
}

In this example you can learn how to assign values to double array at the time of declaration.

/* double Array Example 2 */


/* Save with file name DoubleArray2.java */

public class DoubleArray2


Java Technical Training
{
public static void main(String args[])
{
//DOUBLE ARRAY DECLARATION
double a[] = {100000d,300000d,400000d,786777d};
//DOUBLE ARRAY OUTPUT
System.out.println();
System.out.println("double Array Example");
System.out.println("==================");
System.out.println();
for(int i=0;i<a.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
}
}

In this example you can learn how to declare double array with other double variables.

/* double Array Example 3 */


/* Save with file name DoubleArray3.java */

public class DoubleArray3


{
public static void main(String args[])
{
//DOUBLE ARRAY DECLARATION
double a[], a2;
//MEMORY ALLOCATION FOR DOUBLE ARRAY
a = new double[4];
//ASSIGNING ELEMENTS TO DOUBLE ARRAY
a[0] = 100000d;
a[1] = 300000d;
a[2] = 400000d;
a[3] = 786777d;
a2 = 333333d;
//DOUBLE ARRAY OUTPUT
System.out.println();
System.out.println("double Array Example");
System.out.println("==================");
System.out.println();
System.out.println("a2 value is : "+a2);
System.out.println();
for(int i=0;i<a.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
}
Java Technical Training
}

In this example you can learn how to assign double array to other double array.

/* double Array Example 4 */


/* Save with file name DoubleArray4.java */

public class DoubleArray4


{
public static void main(String args[])
{
//DOUBLE ARRAY DECLARATION
double[] a, a2; //a AND a2 ARE ARRAY VARIABLES
//MEMORY ALLOCATION FOR DOUBLE ARRAY
a = new double[4];
//ASSIGNING ELEMENTS TO DOUBLE ARRAY
a[0] = 100000d;
a[1] = 300000d;
a[2] = 400000d;
a[3] = 786777d;
//ASSIGNING a ARRAY TO a2 ARRAY VARIABLE
a2 = a;
//DOUBLE ARRAY OUTPUT
System.out.println();
System.out.println("double Array Example");
System.out.println("==================");
System.out.println();
System.out.println("a array values");
System.out.println();
for(int i=0;i<a.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a[i]);
}
System.out.println();
System.out.println("a2 array values");
for(int i=0;i<a2.length;i++)
{
System.out.println("Element at Index : "+ i + " " + a2[i]);
}
}
}

Das könnte Ihnen auch gefallen