amp-web-push-widget button.amp-subscribe { display: inline-flex; align-items: center; border-radius: 5px; border: 0; box-sizing: border-box; margin: 0; padding: 10px 15px; cursor: pointer; outline: none; font-size: 15px; font-weight: 500; background: #4A90E2; margin-top: 7px; color: white; box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.5); -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } /** * Jetpack related posts */ /** * The Gutenberg block */ .jp-related-posts-i2 { margin-top: 1.5rem; } .jp-related-posts-i2__list { --hgap: 1rem; display: flex; flex-wrap: wrap; column-gap: var(--hgap); row-gap: 2rem; margin: 0; padding: 0; list-style-type: none; } .jp-related-posts-i2__post { display: flex; flex-direction: column; /* Default: 2 items by row */ flex-basis: calc( ( 100% - var(--hgap) ) / 2 ); } /* Quantity qeuries: see https://alistapart.com/article/quantity-queries-for-css/ */ .jp-related-posts-i2__post:nth-last-child(n+3):first-child, .jp-related-posts-i2__post:nth-last-child(n+3):first-child ~ * { /* From 3 total items on, 3 items by row */ flex-basis: calc( ( 100% - var(--hgap) * 2 ) / 3 ); } .jp-related-posts-i2__post:nth-last-child(4):first-child, .jp-related-posts-i2__post:nth-last-child(4):first-child ~ * { /* Exception for 4 total items: 2 items by row */ flex-basis: calc( ( 100% - var(--hgap) ) / 2 ); } .jp-related-posts-i2__post-link { display: flex; flex-direction: column; row-gap: 0.5rem; width: 100%; margin-bottom: 1rem; line-height: 1.2; } .jp-related-posts-i2__post-link:focus-visible { outline-offset: 2px; } .jp-related-posts-i2__post-img { order: -1; max-width: 100%; } .jp-related-posts-i2__post-defs { margin: 0; list-style-type: unset; } /* Hide, except from screen readers */ .jp-related-posts-i2__post-defs dt { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(1px, 1px, 1px, 1px); white-space: nowrap; } .jp-related-posts-i2__post-defs dd { margin: 0; } /* List view */ .jp-relatedposts-i2[data-layout="list"] .jp-related-posts-i2__list { display: block; } .jp-relatedposts-i2[data-layout="list"] .jp-related-posts-i2__post { margin-bottom: 2rem; } /* Breakpoints */ @media only screen and (max-width: 640px) { .jp-related-posts-i2__list { display: block; } .jp-related-posts-i2__post { margin-bottom: 2rem; } } /* Container */ #jp-relatedposts { display: none; padding-top: 1em; margin: 1em 0; position: relative; clear: both; } .jp-relatedposts:after { content: ''; display: block; clear: both; } /* Headline above related posts section, labeled "Related" */ #jp-relatedposts h3.jp-relatedposts-headline { margin: 0 0 1em 0; display: inline-block; float: left; font-size: 9pt; font-weight: bold; font-family: inherit; } #jp-relatedposts h3.jp-relatedposts-headline em:before { content: ""; display: block; width: 100%; min-width: 30px; border-top: 1px solid #dcdcde; border-top: 1px solid rgba(0,0,0,.2); margin-bottom: 1em; } #jp-relatedposts h3.jp-relatedposts-headline em { font-style: normal; font-weight: bold; } /* Related posts items (wrapping items) */ #jp-relatedposts .jp-relatedposts-items { clear: left; } #jp-relatedposts .jp-relatedposts-items-visual { margin-right: -20px; } /* Related posts item */ #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post { float: left; width: 33%; margin: 0 0 1em; /* Needs to be same as the main outer wrapper for Related Posts */ box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; } #jp-relatedposts .jp-relatedposts-items-visual .jp-relatedposts-post { padding-right: 20px; filter: alpha(opacity=80); -moz-opacity: .8; opacity: .8; } #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post:nth-child(3n+4), #jp-relatedposts .jp-relatedposts-items-visual .jp-relatedposts-post:nth-child(3n+4) { clear: both; } #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post:hover .jp-relatedposts-post-title a { text-decoration: underline; } #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post:hover { filter: alpha(opacity=100); -moz-opacity: 1; opacity: 1; } /* Related posts item content */ #jp-relatedposts .jp-relatedposts-items-visual h4.jp-relatedposts-post-title, #jp-relatedposts .jp-relatedposts-items p, #jp-relatedposts .jp-relatedposts-items time { font-size: 14px; line-height: 20px; margin: 0; } #jp-relatedposts .jp-relatedposts-items-visual .jp-relatedposts-post-nothumbs { position:relative; } #jp-relatedposts .jp-relatedposts-items-visual .jp-relatedposts-post-nothumbs a.jp-relatedposts-post-aoverlay { position:absolute; top:0; bottom:0; left:0; right:0; display:block; border-bottom: 0; } #jp-relatedposts .jp-relatedposts-items p, #jp-relatedposts .jp-relatedposts-items time { margin-bottom: 0; } #jp-relatedposts .jp-relatedposts-items-visual h4.jp-relatedposts-post-title { text-transform: none; margin: 0; font-family: inherit; display: block; max-width: 100%; } #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post .jp-relatedposts-post-title a { font-size: inherit; font-weight: normal; text-decoration: none; filter: alpha(opacity=100); -moz-opacity: 1; opacity: 1; } #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post .jp-relatedposts-post-title a:hover { text-decoration: underline; } #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post img.jp-relatedposts-post-img, #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post span { display: block; max-width: 90%; overflow: hidden; text-overflow: ellipsis; } #jp-relatedposts .jp-relatedposts-items-visual .jp-relatedposts-post img.jp-relatedposts-post-img, #jp-relatedposts .jp-relatedposts-items-visual .jp-relatedposts-post span { height: auto; max-width: 100%; } #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post .jp-relatedposts-post-date, #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post .jp-relatedposts-post-context { opacity: .6; } /* Hide the date by default, but leave the element there if a theme wants to use css to make it visible. */ .jp-relatedposts-items .jp-relatedposts-post .jp-relatedposts-post-date { display: none; } /* Behavior when there are thumbnails in visual mode */ #jp-relatedposts .jp-relatedposts-items-visual div.jp-relatedposts-post-thumbs p.jp-relatedposts-post-excerpt { display: none; } /* Behavior when there are no thumbnails in visual mode */ #jp-relatedposts .jp-relatedposts-items-visual .jp-relatedposts-post-nothumbs p.jp-relatedposts-post-excerpt { overflow: hidden; } #jp-relatedposts .jp-relatedposts-items-visual .jp-relatedposts-post-nothumbs span { margin-bottom: 1em; } /* List Layout */ #jp-relatedposts .jp-relatedposts-list .jp-relatedposts-post { clear: both; width: 100%; } #jp-relatedposts .jp-relatedposts-list .jp-relatedposts-post img.jp-relatedposts-post-img { float: left; overflow: hidden; max-width: 33%; margin-right: 3%; } #jp-relatedposts .jp-relatedposts-list h4.jp-relatedposts-post-title { display: inline-block; max-width: 63%; } /* * Responsive */ @media only screen and (max-width: 640px) { #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post { width: 50%; } #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post:nth-child(3n) { clear: left; } #jp-relatedposts .jp-relatedposts-items-visual { margin-right: 20px; } } @media only screen and (max-width: 320px) { #jp-relatedposts .jp-relatedposts-items .jp-relatedposts-post { width: 100%; clear: both; margin: 0 0 1em; } #jp-relatedposts .jp-relatedposts-list .jp-relatedposts-post img.jp-relatedposts-post-img, #jp-relatedposts .jp-relatedposts-list h4.jp-relatedposts-post-title { float: none; max-width: 100%; margin-right: 0; } } /* * Hide the related post section in the print view of a post */ @media print { .jp-relatedposts { display:none ; } }.wp-block-jetpack-rating-star span:not([aria-hidden="true"]) { display: none; } .amp-logo amp-img{width:190px} .amp-menu input{display:none;}.amp-menu li.menu-item-has-children ul{display:none;}.amp-menu li{position:relative;display:block;}.amp-menu > li a{display:block;} /* Inline styles */ ins.acss263b2{display:block;}span.acssce51c{color:#808000;}figure.acssa5c39{max-width:428px;}span.acssb4d53{color:#993300;}div.acss21998{clear:both;float:left;margin:0 0 20px 0;max-width:100%;}ins.acss6cf41{display:block;text-align:center;}figure.acss93094{max-width:378px;}figure.acss3abf2{max-width:448px;}span.acss7638c{color:#999999;}div.acss138d7{clear:both;}div.acssf5b84{--relposth-columns:3;--relposth-columns_m:2;--relposth-columns_t:2;}div.acssf9d22{aspect-ratio:1/1;background:transparent url(https://code4coding.com/wp-content/uploads/2022/07/pascal-tri1.jpg) no-repeat scroll 0% 0%;height:150px;max-width:150px;}div.acss6bdea{color:#333333;font-family:Arial;font-size:12px;height:75px;}div.acssa0229{aspect-ratio:1/1;background:transparent url(https://code4coding.com/wp-content/uploads/2017/11/3darray-300x176.jpg) no-repeat scroll 0% 0%;height:150px;max-width:150px;}div.acss915c9{aspect-ratio:1/1;background:transparent url(https://code4coding.com/wp-content/uploads/2024/11/array-300x134.jpg) no-repeat scroll 0% 0%;height:150px;max-width:150px;}a.acss9bfd5{font-size:14.52427184466pt;}a.acssc37f8{font-size:16.427184466019pt;}a.acss29e97{font-size:16.631067961165pt;}a.acss361c8{font-size:17.174757281553pt;}a.acss51c7b{font-size:20.029126213592pt;}a.acssa2e10{font-size:20.097087378641pt;}a.acss5dd67{font-size:21.728155339806pt;}a.acssf0e8e{font-size:12.077669902913pt;}a.acss759e3{font-size:17.922330097087pt;}a.acss0abf8{font-size:21.252427184466pt;}a.acss2c000{font-size:13.300970873786pt;}a.acss349b0{font-size:10.038834951456pt;}a.acssf23c5{font-size:8pt;}a.acss7e0a8{font-size:9.2233009708738pt;}a.acsse6f77{font-size:16.970873786408pt;}a.acssc51bb{font-size:14.116504854369pt;}a.acss38f57{font-size:11.26213592233pt;}a.acss066f0{font-size:22pt;}a.acss4e811{font-size:17.31067961165pt;}a.acssfccbf{font-size:19.961165048544pt;}a.acss9cc90{font-size:12.417475728155pt;}a.acss01721{font-size:15.339805825243pt;}a.acsse9f66{font-size:15.543689320388pt;}a.acss72254{font-size:20.708737864078pt;}a.acsseedeb{font-size:20.776699029126pt;}a.acss25b87{font-size:14.320388349515pt;}a.acss7c517{font-size:12.757281553398pt;}a.acss6bf84{font-size:13.504854368932pt;}a.acss639cb{font-size:18.873786407767pt;}a.acss72378{font-size:18.126213592233pt;}a.acss551d3{font-size:16.291262135922pt;} .icon-widgets:before {content: "\e1bd";}.icon-search:before {content: "\e8b6";}.icon-shopping-cart:after {content: "\e8cc";}
Array

Single dimensional Array in Java language

Single dimensional Array in Java language

Single dimension Array in Java language

We will learn about Single dimensional Array in Java language

The array is a data type in Java. It is a collection of similar type of elements that have contiguous index based memory location. We can use one-dimensional array to store a fixed set of elements(single dimension) in Java programming language.

 

Single dimension array in Java

Always, Array index is starting from 0, not 1

Array index ending at n-1(n denotes array length)

Array length(size) starts from 1 and ends at n(n is array length)

 

Array declaration, creation, initiation

Declaration of single dimension Array in Java

Syntax

We can declare one dimension Array following 3 ways

method_1      -   data_type[] array_name;
method_2      -   data_type []array_name;
method_3      -   data_type array_name[];

 

Examples

int[] number;    //Exmple 1
int []number;  //integer array to save only integer value
int number[];

 

double[] zsore;  //Exmple 2
double []zsore;   //double array to save only floating point value
double zsore[];

 

String[] name;   //Exmple 3
String []name;    //String array to save only String value
String name[];
  • The data type can be a primitive data type similar int, char, double, byte etc.. or an object
  • An array name is an identifier

 

Creating a single dimension Array in Java

We can create one dimensional Array as follows

syntax

array_name=new data_type[array_length];

Example

number=new int[5];

Here, the number is an array name that can hold five number of integer values

 

Declaration and creation in single line

We can declare and create one dimensional Array in one line as follows

syntax

data_type []array_name=new data_type[array_length];

Example

int []number=new int[5];

Memory representation of this array

Single dimensional int type Array in Java

Initialization of single dimension Array in Java

We can initialize one dimensional Array as follows- index based

Method 1

Syntax

array_name[0]=element_1;       
array_name[1]=element_2;
array_name[2]=element_3;
array_name[3]=element_4;

 

Example

number[0]=23;  //first element of array 
number[1]=45;  //second element of array 
number[2]=76;  //third element of array 
number[3]=25;  //forth element of array

 

Method 2

Syntax

int[]  array_name={element_1, element_2, element_3,........., element_n }

Example

int[] marks={34,67,84,25,68,98}

typically, array length is determined by the number of values. – array length  of this array is 6

At this example, we provided six values, so array length is 6, this array can hold 6 values

array length cannot be changed in the program

Memory representation of this array

Array declaration single dim array

 

Default values of the array

After the array is declared, (before to initialize) array will be filled every array index with default values in memory locations.
the default value is 0 for numeric type, null for String and false for boolean type

Example

class Array_Default{
public static void main(String args[]){
String[] names=new String[5];//array is declared, but not initilized

System.out.println(names[0]);
System.out.println(names[1]);
System.out.println(names[2]);
System.out.println(names[3]);
System.out.println(names[4]);
}

}

When the above code is executed, it produces the following results:

null
null
null
null
null

 

Display array elements after initialization

Program 1

class array_1dim{
public static void main (String args[]){
int x[]=new int[5]; //diclaration and creation
x[0]=35;        //initialization array element
x[1]=30;
x[2]=13;
x[3]=31;
x[4]=113;
//display every array element
System.out.println("element of array[0] is :"+x[0]);
System.out.println("element of array[1] is :"+x[1]);
System.out.println("element of array[2] is :"+x[2]);
System.out.println("element of array[3] is :"+x[3]);
System.out.println("element of array[3] is :"+x[4]);
}
}

 

When the above code is executed, it produces the following results:

element of array[0] is :35
element of array[1] is :30
element of array[2] is :13
element of array[3] is :31
element of array[4] is :113

 

Program 2

class Array_demo{
public static void main (String args[]){
int[] arr;//array declaration
arr=new int[10]; //array creation, array length - 10 integer type
arr[0]=124;//array initialization - firstt element
arr[1]=24;
arr[2]=14;
arr[3]=12;
arr[4]=104;
arr[5]=120;
arr[6]=129;
arr[7]=195;
arr[8]=145;
arr[9]=624;                            //last element

//display array elements 
System.out.println("Element of index[0] is :"+arr[0]);
System.out.println("Element of index[1] is :"+arr[1]);
System.out.println("Element of index[2] is :"+arr[2]);
System.out.println("Element of index[3] is :"+arr[3]);
System.out.println("Element of index[4] is :"+arr[4]);
System.out.println("Element of index[5] is :"+arr[5]);
System.out.println("Element of index[6] is :"+arr[6]);
System.out.println("Element of index[7] is :"+arr[7]);
System.out.println("Element of index[8] is :"+arr[8]);
System.out.println("Element of index[9] is :"+arr[9]);
}

}

 

When the above code is executed, it produces the following results:

element of index[0] is :124
element of index[1] is :24
element of index[2] is :14
element of index[3] is :12
element of index[4] is :104
element of index[5] is :120
element of index[6] is :129
element of index[7] is :195
element of index[8] is :145
element of index[9] is :624

 

Display of element of the array using for loop

we can display elements from the array using for loop as follows

for (int i=1; i<=5; i++){
System.out.print(array_name(i));
}

program 1

class arraywithfor{
public static void main(String args[]){
int[] num;          //declaration of Array 
num=new int[6];     // Creation of Array
num[0]=115;         //initiation of element for index 0
num[1]=172;         //initiation of element for index 1
num[2]=193;         //initiation of element for index 2
num[3]=607;         //initiation of element for index 3
num[4]=745;         //initiation of element for index 4
num[5]=100;         //initiation of element for index 5
System.out.println("Element of Array");
for(int i=0; i<=5; i++){
    System.out.println(num[i]);
    //Display array element using for loop
}
}

 

When the above code is executed, it produces the following results:

Element of Array
115
172
193
607
745
100

 

Display of element using  for loop in Array with array length

Method 1

for(i=0; i<=array.length-1; i++) {      //i<=array.length-1
System.out.println(array_name[i]);
}

 

Method 2

for(i=0; i<array.length; i++) {           //i<array.length
System.out.println(array_name[i]);
}

Program 1

class array1d{
public static void main (String args[]){
int anArray[]=new int[6];
anArray[0]=345;
anArray[1]=145;
anArray[2]=365;
anArray[3]=349;
anArray[4]=355;
anArray[5]=745;

for(int i=0; i<=anArray.length-1; i++){
    System.out.println(anArray[i]);
}
    }
        }

 

When the above code is executed, it produces the following results:

345
145
365
349
355
745

 

 

Display of element using Enhanced for loop in Array

we can display elements from the array using enhanced for loop as follows

for int i:array_name{
System.out.print(i);
}

Program 1

class arrayforloop2{
public static void main(String args[]){
int[] num;//declaration of Array 
num=new int[6];// Creation of Array
num[0]=110;//initiation of element for index 0
num[1]=142;//initiation of element for index 1
num[2]=153;//initiation of element for index 2
num[3]=607;//initiation of element for index 3
num[4]=745;//initiation of element for index 4
num[5]=100;//initiation of element for index 5
System.out.println("Element of Array");
for(int i:num){
    System.out.println(i);
    //Display array element using for loop
}
}

 

When the above code is executed, it produces the following results:

Element of Array
110
142
153
607
745
100

 

 

Array and while loop in Java

we can display elements from the array using while loop as follows

while(i<=5){
System.out.print(Array_name[i]);
}

Program 1

class Array_whileloop{
    public static void main(String args[]){
        int marks[]={45,67,65,82,49,97}; // Array declaration and initiation
        int i=0;  //Start with zero for first array index
            while(i<=5){//last Array index 5
                System.out.println(marks[i]);
                i++;

        }
    }
}

 

When the above code is executed, it produces the following results:

45
67
65
82
49
97

 

Similar post

Array in Programming language

One dimensional array in C language

One dimensional array in C++ language

Two dimension array in Java language

Two dimension array in C language

Two dimension array in C++ language

Three dimension array in Java language

Three dimension array in C language

Three dimension array in C++ language

 

Suggested for you

for loop in Java language

while loop in Java language

Do-while loop in Java language

Enhanced for loop in Java language

 

Datatype and variables in Java programming language
Array data type in programming languages
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

Write a Python program to find the first n prime numbers

Write a Python program to find the first n prime numbers In this article we…

7 days ago

Python: Calculate Average of odd and even in a list using loops

Python: Calculate Average of odd and even in a list using loops In this post,…

1 week ago

Python: Average of Odd & Even Numbers from User Input

Python: Average of Odd & Even Numbers from User Input In this post, we will…

2 weeks ago

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

3 months ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

3 months ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

3 months ago