Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Some array operations (declare, initialize, reassign) 
 

In this section we will learn how to wok with array of object type and also with primitive type.

 

Some array operations (declare, initialize, reassign)

                         

In this section we will learn how to wok with array of object type and also with primitive type. In code code given below we will declare array in different ways for both object and primitive type. After declaration, initialize with primitive type values and objects then reassign values in some declared arrays.

 

 

 

 

 

ArrayOperation.java

public class ArrayOperation {
    public static void main(String[] args) {
        // declare array of object type
        Array[] arr1; // uninitialized array variable
        Array[] arr2 = new Array[5]; // initialize with Null references
        Array[] arr3 = new Array[4];
        // initialize array with object of Array type.
        arr1 = new Array[]{new Array(), new Array()};
        //print the length of three declared arrays
        System.out.println("Length of array 'arr1' : " + arr1.length);
        System.out.println("Length of array 'arr2' : " + arr2.length);
        System.out.println("Length of array 'arr3' : " + arr3.length);
        // print the elements of array 'arr2'
        System.out.println("Elements of array 'arr2'.");
        for (int i = 0; i < arr2.length; i++) {
            System.out.println("b[" + i + "]=" + arr2[i]);
        }
        // assign arr2 to array arr1
        arr1 = arr2;
        System.out.println("Now length of array 'arr1' : " + arr1.length);
        // Arrays of primitives:
        int[] a; // Null reference
        int[] b = new int[5];
        int[] c = new int[4];
        int[] d = {11, 47, 93};
        // fill values in integer array 'c'
        for (int i = 0; i < c.length; i++) {
            c[i] = i;
        }
        
        // print length of arrays of primitive type
        System.out.println("Length of integer array 'b' = " + b.length);
        System.out.println("Length of integer array 'c' = " + c.length);
        System.out.println("Length of integer array 'd' = " + d.length);
        // print all the elements of array 'b'
        for (int i = 0; i < b.length; i++) {
            System.out.println("f[" + i + "]=" + b[i]);
        }
        // assign array 'd' to array 'a' and print the length of array 'a'
        a = d;
        System.out.println("Now length of array 'a' : " + a.length);
        // assign some new values in array 'a' and then print array 'a'.
        a = new int[]{1, 2};
        System.out.println("Final length of array 'a' : " + a.length);
    }
}
class Array {
}

Output :

Length of array 'arr1' : 2
Length of array 'arr2' : 5
Length of array 'arr3' : 4
Elements of array 'arr2'.
b[0]=null
b[1]=null
b[2]=null
b[3]=null
b[4]=null
Now length of array 'arr1' : 5
Length of integer array 'b' = 5
Length of integer array 'c' = 4
Length of integer array 'd' = 3
f[0]=0
f[1]=0
f[2]=0
f[3]=0
f[4]=0
Now length of array 'a' : 3
Final length of array 'a' : 2

Download Source Code

                         

» View all related tutorials
Related Tags: java c string ide array io word vi new key this id element elements for example keyword with to ini

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.