Friday, November 17, 2017

While Loop in Java : One Dimensional Array | Tow Dimensional Array | Multidimensional array java

While Loop in java

A while loop is a control structure that allows you to repeat a task a certain number of times. On the off chance that the quantity of emphasis isn't settled, it is prescribed to utilize while circle.

How while Loop works?

In while circle, the condition is assessed first and on the off chance that it returns genuine then the announcements inside while circle execute. At the point when condition returns false, the control leaves circle and hops to the following explanation after while circle.

While Loop java, java while, One Dimensional Array, Tow Dimensional Array, Multidimensional array java, java while loop example, one dimensional array java example, two dimensional array java example, Multidimensional  dimensional array java example etc.


While Loop Java Example

Syntax:
while(condition)
{             
//code to be executed  
}  

public class my 
{
public static void main(String[] args)
      {             
                  int d=1;  
                  while(d<=5)
                  {  
                              System.out.println(d);  
                              i++;  
                  }  
      }  
}  

Output:
1
2
3
4
5

Array using while loop
  • One Dimensional Array
  • Two Dimensional Array 
  • Jagged Array
A cluster is a gathering of like-wrote factors that are alluded to by a typical name. Varieties of any sort can be made and may have at least one measurements. A particular component in an exhibit is gotten to by its file. Clusters offer helpful methods for gathering related data. 

A one-dimensional cluster is, basically, a rundown of like-wrote factors. To make an exhibit, you initially should make a cluster variable of the coveted sort. The general type of a one-dimensional cluster affirmation is

           type var-name[ ];
           int month_days[];

          Array Declaration Syntax

There is a second form that may be used to declare an array:

           type[ ] var-name;
           int [] months_days;

           int a [] = new int[3];
           int[] a2 = new int[3];

           char twod1[][] = new char[3][4];
           char[][] twod2 = new char[3][4];

           int[] nums, nums2, nums3; // create three arrays
           int nums[], nums2[], nums3[]; // create three arrays

           int[] myArray = new int[3];
           int[] myArray = {1,2,3};
           int[] myArray = new int[]{1,2,3}

1. One Dimensional Array with Example:

Following Java Program ask the client to enter the cluster size and afterward solicit to enter the component from the exhibit to store the components in the cluster, at that point at last show the cluster components on the example.

class my
{
public static void main(String ar[])
            {
            int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30, 31 };
            System.out.println("April has " + month_days[3] + " days.");
            }
}

Output:
April has 30 days

2. Tow Dimensional Array with Example:

The two-dimensional exhibit can be made in Java Programming dialect by utilizing the two circles, the first is an external circle and the second one is the internal circle. The external circle is in charge of lines and the internal circle is in charge of segments. Also, the two lines and sections consolidate to make two-dimensional (2D) Arrays.

public class my 
{
public static void main(String[] args)
{
int[][] values = new int[4][4];

                         values[0][0] = 1;
                         values[1][1] = 2;
                         values[3][2] = 3;

       
                  for (int i = 0; i < values.length; i++) 
                         {           
                                    int[] sub = values[i];
                                    for (int x = 0; x < sub.length;x++)                  
                                        {  
                                        System.out.print(sub[x] + " ");
                                        }
                                        System.out.println();
                        }
}
}

3. Jagged Array (Multidimensional Array Java) with Example:

The rough exhibit is the cluster of clusters to such an extent that part exhibits can be of
Various sizes, i.e., we can make a 2-D exhibits yet with the variable number of sections in 
Each column. These kinds of clusters are otherwise called jagged exhibits. 

Public class my
{
           Public static void main(String args[])
          {
                    int twoD[][] = new int[4][];
                    twoD[0] = new int[1];
                    twoD[1] = new int[2];
                    twoD[2] = new int[3];
                    twoD[3] = new int[4];

for (int i = 0; i < 4; i++)
                        {
                              for (int j = 0; j < i + 1; j++)
                              {
                                     twoD[i][j] = i + j;
                              }
                        }
                        for (int i = 0; i < 4; i++)
                        {
                              for (int j = 0; j < i + 1; j++)
                              System.out.print(twoD[i][j] + " ");
                              System.out.println();
                        }
            }

Output:
1 2 
2 3 4 
3 4 5 6 


No comments:

Post a Comment

Top 200+ Core Java Interview Questions And Answers For 2019

Most Popular Java Interview Questions Given below is a comprehensive list of most important and commonly asked basic and advanced Java p...