IT Hardwares

eg: UK or Brides UK or Classical Art or Buy Music or Spirituality
 
eg: UK or Brides UK or Classical Art or Buy Music or Spirituality
 
Business & Money
Technology
Women
Health
Education
Family
Travel
Cars
Entertainment
SD Editorials
Online Guide and article directory site.
Foodeditorials.com
Over 15,000 recipes & editorials on food.
Lyricadvisor.com
Get 100,000 Lyric & Albums.
  • Business & Money
    • A Guide to Business
    • Guide to Finance
    • Ideas for Marketing
    • Legal Guide
    • Guide to Insurance
    • Lettre De Motivation
    • Guide to the Stock Market
    • Human Resource Career
    • Sales Marketing
    • Forex & Trading
    • Advertising & Marketing
    • Startup Guide
  • Technology
    • Guide to Technology
    • Cell Phones
    • Computer Software
    • IT Hardwares
    • Internet
    • Online Security
    • Cameras
    • Search Engine Optimization
    • Science & Technology
  • Women
    • Guide to Women
    • Relationship Advice
    • Marriage
    • Jewelry
    • Pregnancy
    • Fashion Style
    • Divorce Guide
    • Wedding Guide
    • Dating Guide
    • Natural Beauty
  • Health
    • Guide to Health
    • Guide to Medical
    • Plastic Surgery
    • Weight Loss
    • Sports
    • Body Wellness
    • Cancer Treatment
    • Common Illness
    • Health & Lifestyle
  • Education
    • Military Service
    • Politics and Policy
    • Arts & Humanities
    • Education and Teaching
    • Learn Languages
    • Colleges & Universities
  • Family
    • Quality Home Improvement
    • Hobbies and Interests
    • Family Guide to
    • Pet Guide
    • Loans Guide
    • Credit Cards
    • Gardening Guide
    • Home Security
    • Real Estate
    • Home Decor
    • Gift & Present
  • Travel
    • The Travel Guide
    • Adventure Travel
    • Cruise Ships
    • Beach Holiday
    • Travel Accommodation
    • Holiday Destinations
  • Cars
    • Information on Cars
    • Traffic Violations
    • Auto Insurance
    • Trailers
    • Sport Cars
    • The Bikes
  • Entertainment
    • Entertainment Guide
    • World Music
    • Photo & Video
    • Television & Games

Java Arrays

    View: 
Each and every element in an array is identified through a non negative number called index. The size of an array is fixed and can not be changed



An array has a final field called length, which denotes the number of elements an array can accommodate. The first element is always at index 0 and the last element at length – 1.

Declaring an array variable

An array can be declared using following syntax.

[] arrayName ;

arrayName [];

Here type can be any primitive or reference type.

Note: Array size is not defined here. This array variable can be assigned to an array of any length.

Declaring an array variable does not actually create an array nor allocate any memory. It just creates a variable which can hold reference to the array.

An array variable declared as a member variable will be automatically initialized to default value null, while local array variable not.

Creating an Array

Since arrays are java objects, it can be created using new operator using following syntax.

arrayName = new [];

Here size must be byte, char, short or int. Any attempt to create an array with long will result in compilation error. If the array size is negative, NegativeArraySizeException is thrown.

When create arrays are initialized automatically to their default values.

Note: In java, it is perfectly legal to create array with size 0.

Java array creation examples are given below.

myIntArray = new int[10];

myStringArray = new String[10];

myObjectArray = new MyObject[10];

Array declaration and creation can be combined into one statement as follows.

int myIntArray = new int[10];

String myStringArray = new String[10];

MyObject myObjectArray = new MyObject[10];

Initializing an array

Once created, array elements can be initialized explicitly using loop or initialization can be combined with the array creation using following syntax.

[] arrayName = { };

Following is the example of the same.

int [] myIntArray = {1,2,3,4};

Note: initialization list can be legally terminated by comma. For example

int [] myIntArray = {1,2,3,4,};

Accessing elements of an array

Individual elements of an array can be accessed by specifying index with [] operator, with the following syntax.

arrayName[]

Here index can be any expression, which evaluates to non negative int value. Array index always starts at 0 and ends at arrayName.length – 1. Array index is automatically checked to ensure that it is well within the bounds. If index is less than 0 or grater than arrayName.length, an ArrayIndexOutOfBoundException is thrown.

Note: [] operator is not used when manipulating the array reference. For example, when array reference is passed to another method.

Multidimensional Array

In java, multidimensional ( array of arrays) can be defined using following syntax.

arrayName[]…[];

[]…[] arrayName[];

Where []…[] denotes number of dimension.

We can also combine array declaration and array construction in one statement as follows.

[]…[] arrayName = new []…[];

Example of multidimensional array

int myIntArray[][] = new int[5][5];

String myStringArray[][] = new String[5][5];

Multidimensional can also be created and initialized in single expression as follows.

int myIntArray[][] = {

{1,2,3},

{4,5,6}

Note: While constructing multidimensional array, length of deeply nested array can be omitted which will not be constructed.

For example,

int myIntArray[][][] = new int[1][2][];

Deeply nested array can be constructed later on using loop.

Example program of Multidimensional array

Class MultidimensionalArrayExample{

Public static void main(String args[]){

int myIntArray[][] = { {1,2},{3,4}};

for(int i = 0; i < myIntArray.length; i++){

for(int j=0; j < myIntArray[i].length; j++){

System.out.println(myIntArray[i][j]);

}

}

}

The above given program will print elements of multidimensional array to console.

Anonymous Array

In java it is perfectly legal to create an anonymous array using the following syntax.

new [] { };

Anonymous array example

new int[]{1,2,3};

The above given example creates a nameless array and initializes it. Here, neither name of the array nor the size is specified. It also creates a array which can be assigned to reference or can be passed as a parameter to any method.

Example of Anonymous array

public AnonymousArrayExample{

public static void main(String args[]){

System.out.println(“Length of array is “ + findLength(new int[]{1,2,3}));

public static findLength(int[] array){

return array.lentgh;

The above given program will print the length of the anonymous array which is being passed to the static method.
More Articles from
Computer Hardware Guide Pg501
Buying And Selling Websites
Cd Packaging And Duplication
Graphic And Design Software
Help To Get Organized
Management And Business Administration
Pimp Up My Profile
Professional Website Design Company
Songs I Didnt Write
Spy Stuff For Kids
To Build Your Own Web Site
Used Studio Lighting Equipment
Web Design Style Sheets
Whole House Surge Suppressor
Windows Xp Professional Review
Profitable Knowledge: Adding Info Products To Your Offerings
Professional Traffic Building Tips
Productive High Ticket Coaching Selling - Revealed - 6 Steps to Improve Your High Ticket Coaching Selling
Productive How to Increase Your Prices - Discover 6 Methods to Make Money Through Increasing Your Prices
Profitable Coaching - Revealed - 4 Steps to Excel With Coaching
Profitable Coaching - Announcing 3 Steps to Make Money Through Coaching
» More on
Computer Hardware Guide
  • Related Articles
  • Author
  • Most Popular
Rahim Vindhani has sinced written about articles on various topics from Computers and The Internet. . Rahim Vindhani's top article generates over 5400 views. to your Favourites.
Check Your School Grades
Follow these 10 tips and within next to no time the quality of your sleep will improve, you will feel much more awake and active during the day and your grades will improve
 
A Guide to Business | Guide to Technology | Guide to Women | Guide to Health | Family Guide to | Travel & Vacations | Information on Cars

EditorialToday IT Hardwares has 2 sub sections. Such as Computer Guide and Hardware. With over 20,000 authors and writers, we are a well known online resource and editorial services site in United Kingdom, Canada & America . Here, we cover all the major topics from self help guide to A Guide to Business, Guide to Finance, Ideas for Marketing, Legal Guide, Lettre De Motivation, Guide to Insurance, Guide to Health, Guide to Medical, Military Service, Guide to Women, Pet Guide, Politics and Policy , Guide to Technology, The Travel Guide, Information on Cars, Entertainment Guide, Family Guide to, Hobbies and Interests, Quality Home Improvement, Arts & Humanities and many more.
About Editorial Today | Contact Us | Terms of Use | Submit an Article | Our Authors