Reading:  

Quick introduction to Java Programming language - Part 1


Working with Arrays

Java supports data structure the array is used to stored fixed size collection of elements of same type. For Example if we need to store number from 1 to 10 instead of declaring individual variable we can declare one array variable having number from 1 to 10.

 

Declaring Array Variables:

To use an array in a program variable should be declared to reference the array and type of array should be mentioned.

dataType[] RefVararray;
double[] SalaryList;


Creating Arrays: 

Array can be created by using new keyword. 

RefVararray = new dataType[arraySize];

The above declaration does below things:

  1. Creates array using new dataType[arraySize];
  2. It assigns reference of newly created array to variable RefVararray.

int[] myUserList = new int[10];

The above statement creates an integer array myUserList with array of 10 elements. The index of the 10 elements will be from 0 to 9.


The Arrays Class:

The java.util.Arrays class contains few static methods for searching and sorting arrays, filling array and comparing arrays. These methods can be overloaded for all primitive types.

 

Methods with Description

public static void sort(Object[] x)

Sorts array of objects into ascending order.

public static void fill(int[] x, intval)

Assigns specified int value to each element of specified array of integer.

public static boolean equals(long[] x1, long[] x2)

if two specified arrays of longs are equal to one anothertrue is  Returned

public static intbinarySearch(Object[] x, Object key)

Binary search algorithm is used to search the object in specified array.

Processing Arrays:

For processing array elements for or foreach loop is used since all elements in the array are of same size and type.

public class ExampleArray {
    public static void main(String[] args) {
		int[] numList = {5,9,8,7};  
		for (inti = 0; i<numList.length; i++) {
			System.out.println(numList[i] + " ");
		}      
	}
}
 

 

Description

This tutorial is targetted for beginers seeking a quick get on with guide on Java programming language. 3 parts include 

  1. Java basics
  2. Object oriented programming
  3. Advance concepts

Each part is subdivided in multiple chapters.

Java basics has the following chapters

  1. Introduction
  2. Environment Setup
  3. Basic Syntax
  4. Objects and Classes
  5. Basic Data Types
  6. Variable Types
  7. Modifier Types
  8. Basic Operators
  9. Loops
  10. Decision Making
  11. Numbers Class
  12. Character and String Class
  13. Arrays
  14. Date and Time
  15. Regular Expression
  16. Methods
  17. Streams, Files and I/O
  18. Exceptions Handling

Thanks for reading and as always, your feedback is very important to us. Let us know how we can improve and if you found any issues with this write up, send us a correction.



Audience

Beginners or students seeking a refresher on Java language

Author: Subject Coach
Added on: 9th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!