Skip to main content

1D Array in JAVA


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.

  1. //first method
  2. Int[] mynums; //here we have declared an array named ‘mynums’
  3. mynums=new int[5]; //here we have given the size of the array name ’mynums’

By using second method we declare the array first and initialize the array in the same line.
  1. //second method
  2. Int[] mynums=new int[];

Step 2:
So the first part is about making an array, below the example will let us know how we work to insert values into the array.
  1. mynums[0] = 2; //zeroth location of array ‘mynums’ will have value 2.
  2. mynums[1] = 3;
  3. mynums[2] = 5;
  4. mynums[3] = 7;
  5. mynums[4]=2;
  6. mynums[5]=23;

We can also simply give in values to the array and array will determine its size itself. For Example

  1. int[] mynums = { 10, 100, 1000 }; //here array named ‘mynums’ will have value 10 at zeroth position,
  2. //100 at the first position w.r.t index which is always at the start of the array.


We can easily retrieve data from array , you can clearly see the example below

  1. System.out.print(mynums[0]);
  2. System.out.print(mynums[1]);
  3. //so on, depending on the number of entries in the array
  4. //for loop can also be used

Code Example

  1. package tutorial;
  2. import java.util.*;
  3. public class myclass {
  4.  
  5. public static void main(String[] args) {
  6. String[] test = { "This", "is", "a","Java","tutorial"}; //input
  7. System.out.print("I am retrieving an element of the array:"); //just for printing
  8. System.out.print(test[1]); // printing out the 1st element(not zeroth)
  9. System.out.println();
  10.  
  11. System.out.print("It is the length of the array:");
  12. System.out.print(test.length); // a function can be used to get //length of the array
  13.  
  14. }
  15.  
  16. }

In this code I have demonstrated how we have worked with the elements of the array, first inserted them and then retrieved them.

Screen Shot

Screen Shot

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.