- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java One Dimensional Array
A one dimensional array is, basically, a list of like-typed variables.
Create One Dimensional Array
To create an array, you first must create an array variable of the desired data type. Here is the general form of a one-dimensional (1D) array declaration :
type var-name[];
Here, type declares the element type (called the base type) of the array. The element type determines the data type of each element that comprises the array. Therefore, the element type for the array determines what type of data the array will hold. For instance, below code fragment declares an array named month_days with the type "array of int" :
int month_days[];
Although this declaration establishes the fact that the month_days is an array variable, no array actually exists. To link month_days with an actual, physical array of integers, then you must allocate one using the new operator/keyword and assign it to month_days. The new is a special operator that allocates memory.
You will learn more about the new operator in later chapter, but you have to use it now to allocate memory for arrays. The general form of new as it applies to one-dimensional (1D) arrays appears as follows :
array-var = new type[size];
Here, type specifies the type of data being allocated, size specifies the number of elements present in the array, and array-var is the array variable that is linked to the array. That is, to use new to allocate an array, you must define the type and the number of elements to allocate.
The elements in the array allocated by the new will automatically be initialized to zero (for the numeric types), false (for boolean), or null (for the reference type, described in a later chapter). This example allocates a 12-element array of integers and links them to month_days:
month_days = new int[12];
When this statement executes, month_days will refer to an array of 12 integers. Further, all the elements in the array will be initialized to zero.
Get an Array
Getting an array is a two-step process. First, you must declare a variable of the desired array type. Secondly, you must allocate the memory that will hold the array, using the new operator, and assign it to the array variable.
When you have allocated an array, you can access a specific element in the array by specifying its index within square brackets. All the arrays indexes starts from zero. For example, the following statement allots the value 28 to the second element of month_days:
month_days[1] = 28;
The next line displays the value stored at index 2:
System.out.println(month_days[2]);
Java One Dimensional Array Example
Putting together all the pieces of code, below is a program that creates an array of the number of days in each month:
/* Java Program Example - Java One-Dimensional (1D) Array */ public class JavaProgram { public static void main(String args[]) { int month_days[]; month_days = new int[12]; month_days[0] = 31; month_days[1] = 28; month_days[2] = 31; month_days[3] = 30; month_days[4] = 31; month_days[5] = 30; month_days[6] = 31; month_days[7] = 31; month_days[8] = 30; month_days[9] = 31; month_days[10] = 30; month_days[11] = 31; System.out.println("April has " + month_days[3] + " days."); } }
When the above Java program is compile and executed, it will produce the following output:
Java array indexes start from zero, so the number of days in April is month_days[3] or 30.
It is possible to combine the declaration of array variable with the allocation of the array itself, shown below
int month_days[] = new int[12];
This is the way that you will generally see it done in professionally written Java programs.
An Array can be initialized when they are declared. The process is much the same as, used to initialize the simple types. An array initializer is a list of comma-separated expressions surrounded through curly braces. The commas separate the values of array elements. The array will automatically created large enough to hold the number of elements that you define in the array initializer. There is no need to use the new operator. For instance, to store the number of days in each month, here this code creates an initialized array of integers :
/* Java Program Example - Java One-Dimensional Array */ /* This is the improved version of the previous program */ public class JavaProgram { public static void main(String args[]) { int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; System.out.println("April has " + month_days[3] + " days."); } }
The output of the above Java program is same as of the previous Java program, i.e.:
Examples
Here are some example programs, uses one dimensional array, that you can go for:
- One Dimensional Array Program
- Linear Search
- Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Reverse Array
- Insert Element in Array
- Delete Element from Array
- Merge two Array
- Bubble Sort
- Selection Sort
- Insertion Sort
« Previous Tutorial Next Tutorial »