Home Java Beginners Java - Array in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java - Array in Java

Advertisement
Array is the most important thing in any programming language.

Array Example - Array in Java

     

Array: Array is the most important thing in any programming language. By definition, array is the static memory allocation. It allocates the memory for the same data type in sequence. It contains multiple values of same types. It also store the values in memory at the fixed size. Multiple types of arrays are used in any programming language such as: one - dimensional, two - dimensional or can say multi - dimensional. 

Declaration of an array:  
int num[]; or int num = new int[2];
Some times user declares an array and it's size simultaneously. You may or may not be define the size in the declaration time. such as:
int num[] = {50,20,45,82,25,63}; 

In this program we will see how to declare and implementation. This program illustrates that the array working way. This program takes the numbers present in the num[] array in unordered list and prints numbers in ascending order. In this program the sort() function of the java.util.*; package is using to sort all the numbers present in the num[] array. The Arrays.sort() automatically sorts the list of number in ascending order by default. This function held the argument which is the array name num.

Here is the code of the program:-

import java.util.*;

public class  array{
 
 public static void main(String[] args){
  int num[] = {50,20,45,82,25,63};
  int l = num.length;
  int i,j,t;
  System.out.print("Given number : ");
  for (i = 0;i < l;i++ ){
  System.out.print("  " + num[i]);
  }
  System.out.println(
"\n");
  System.out.print("Accending order number : ");
  Arrays.sort(num);
  
  for(i = 0;i < l;i++){
  System.out.print("  " + num[i]);
  }
  }
}

Output of the program:

C:\chandan>javac array.java

C:\chandan>java array
Given number : 50 20 45 82 25 63

Ascending order number : 20 25 45 50 63 82

  

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 5, 2007

Related Tutorials

Ask Questions?    Discuss: Java - Array in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
srujana
April 11, 2011
java

write a program to read 2dimensional array using one loop?
Ryan D. Pasaporte
April 13, 2011
Array

Cool!...but how can we develop that program if we want another variable instead?..like you will need to input your own characters?...for example give a number : 1546234 ascending number : 1234456
kenny
April 19, 2011
java code

creating array of 5 students and theier marks for Test 1 up to test 3.Thy are doing diploma and degree and displaying all results
Ritchie F Gimoto
June 21, 2011
Data structure

Im so very thankful that ihave a solution of my assignment.
gaurav
July 3, 2011
what is the default size of an array?

please tell me default size of an array.what is the size of an array
max
August 15, 2011
java

if(choice==2) { System.out.println("Boarding\t\tAlighting"); System.out.println("Bus Stop\t\tBus Stop\t\t Fare\t\tDuration"); System.out.println("---------\t\t---------\t\t--------\t--------"); for (int i = 0; i < counter; i++) { System.out.print(boardingArray[i]); System.out.print(" "); System.out.print("\t"); System.out.print(alightingArray[i]); System.out.print(" "); System.out.print("\t"); System.out.print("$"); System.out.print(fmt.format(busFareArray[i])); System.out.print("\t\t"); System.out.print(busDurationArray[i]/60); System.out.print(" hr "); System.out.print(busDurationArray[i]%60); System.out.println(" minutes"); } first array result not working ???
jerdz _19
August 18, 2011
cs 103

...its so easy to make a program using array,if I study well the codes of array comment........
ashwini kumar singh
August 30, 2011
java

java is a hiegh level language
Nitin Adlakha
June 5, 2013
Array

Nic Program can u snt me more Program in java,....Please Sir If u have 9 Problm,... Nitin
Aniruddh
February 4, 2013
INTEGER ARRAY USER INPUT PROBLEM

hello , i am trying to write a program in which i want to make an integer array where user should provide elements at command prompt ( not command line , simplly). I can do this for accepting string type of values using Bufferedreader object but unable to do same for int variable's array . My problem is related to storage of many elements of runtime in single 1D array . Thanks to you in advance .
amit
February 7, 2013
regarding directory name used in example

please avoid using names like chandan etc i.e. any name use roseindia instead, looks better....
Michael Angelo
November 24, 2011
Java

Nice page.
Raushan kumar singh
November 26, 2011
java

Array is the most important thing in any programming language. By definition, array is the static memory allocation. It allocates the memory for the same data type in sequence. It contains multiple values of same types Raushan Kumar Singh..
George
November 30, 2011
Thank You..

Thank you sir for making this syntax...Your helping many I.T students like me..I'm new in the world of programming, Thanks again sir..keep it up.
shahrukh
December 5, 2011
loops and arry

mail me all example of loops and array in all programing language
markjoe merca
January 11, 2012
java

i want to learn more in java...
aiza alegre
January 16, 2012
programming

whats the rule of array in java programming
sagar
February 6, 2012
java

if() print ("hi"); else print ("Hello"); sir i want to both hi & hello are print but how!
kristy
February 23, 2012
programming 1

nice one
ulysses
February 28, 2012
computer programming

thanks for this site i can the definition of arrays
prince
March 17, 2012
gud

excellent.. i love it..keep it up
sanjay singh kushwaha
April 22, 2012
some mistake in array declaration

Declaration of an array: int num[]; or int num = new int[2]; mistake in second line i.e array of integer type can not be hold in nonArray integer type variable
Rasha
May 12, 2012
Storing images in an array in Java

Hello, I'm new to java and trying to design a game and was wondering if it's possible to store my images in array. If it's possible, how?. Any example and description will be much appreciated. Thanks!
murugan
May 26, 2012
Sort method

How to get the value from user and find the sort (asc or dece)?
harpreet kaur
June 5, 2012
java

it help me in learning java
abc
June 6, 2012
arrays

wht is the use of j and t variables
DON
June 21, 2012
comments

its should be dicussed clearly and explain properly and we need more explation da .....do properly nobody ll nt like this dnt see the example in this website please u ll fail nly...if u want to pass check someother website guys.....this website waste
jonry
July 3, 2012
it203

example of array in 10 input
sly
March 8, 2013
advance programming

nice
Chetan pote
August 21, 2012
java

very nice but my question
veerappan
September 14, 2012
hi

where use int i in pgm and 1=num.length
Bishnu Sharma
September 23, 2012
Comment

Fabulous example to understand numeric array in java.
Berserker
October 12, 2012
java Array in descending order

PLease help me how to create a program that well give in Array in descending order.. even i gave an input a mix number
jess
October 16, 2012
array demo of library system

can you give me some sample of program for library system.
mehvish
June 16, 2013
write a program that print the prime number

i am very thankful if you give correct solution
HARI
June 25, 2013
ARRAY

write a program to accept 'n' no of String inputs using array concept.
Diomar
January 15, 2014
parts of Java Array

please answer my question. What are the parts of java array?? ASAP.. Thank you
DMCA.com