1D Array in JAVA
Submitted by moazkhan on Monday, February 16, 2015 - 07:09.
1D array in JAVA
In this tutorial you will learn 1) What is array in JAVA? 2) Why we use arrays? 3) How we use arrays in JAVA coding? What is an Array? Array is a data structure used in programming languages to store similar type of items. Arrays are the simplest data structure. The contents of array can be quickly accessed and can easily be deleted. Arrays are used to put in independent variables of the same type in them. Array is just like a one directional block having equal sized partitions and each block we have a value stored. Each value in the array is called the element. Compared to C programming the arrays in the JAVA programming are not static rather they have a dynamic nature. Why we use arrays? The arrays help us to store same type of objects in a same place. Each array can be accessed through an index which let us work in a more easy fashion. The index does not contain any value rather it only points towards the location. It provides the faster access in a more organized fashion. Lots of values are stored in a single variable so that the programmer does not need to remember the names of multiple variables. Arrays have a known length, once the size is set, it can not be changed How we use arrays in JAVA coding? Step 1: There are two ways, by using first method we can first declare an array and then in the next part we initialize it.- //first method
- Int[] mynums; //here we have declared an array named ‘mynums’
- mynums=new int[5]; //here we have given the size of the array name ’mynums’
- //second method
- Int[] mynums=new int[];
- mynums[0] = 2; //zeroth location of array ‘mynums’ will have value 2.
- mynums[1] = 3;
- mynums[2] = 5;
- mynums[3] = 7;
- mynums[4]=2;
- mynums[5]=23;
- int[] mynums = { 10, 100, 1000 }; //here array named ‘mynums’ will have value 10 at zeroth position,
- //100 at the first position w.r.t index which is always at the start of the array.
Code Example
- package tutorial;
- import java.util.*;
- public class myclass {
- }
- }
Add new comment
- 169 views