Uses of single dimensional arrays


Here I'm going to explain some uses of single dimensional arrays in java programming


This example is initializing arrays with user input values, following loop initializing array myList with user input values
……

 java.util.Scanner input=new java.util.Scanner(System.in);

double [] myList=new double[10];

System.out.print(“Enther”+myList.lenght+ “values”);

For(int i=0; i < myList.length;i++){

myList[i]=input.nextDouble();

}

System.out.println(myList);

}

Summing an array

You can use this code to get the total of all values in an array, assume that we have a myList array with 1-10 values
package hellowjava;
public class Hellowjava {
public static void main(String[] args) {
   int myList[]={1,2,3,4,5,6,7,8,9,10};
int tot=0;
     for (int i = 0; i < myList.length; i++)
     { 
       tot += myList[i];
       }
System.out.println(tot); }}

How to find smallest number in an array

 
package hellowjava;
public class Hellowjava {
    public static void main(String[] args) {
        int[]myArray= new int[10];
        myArray[0]=1;
        myArray[1]=2;
        myArray[2]=3;
        myArray[3]=4;
        myArray[4]=5;
        myArray[5]=6;
        myArray[6]=7;
        myArray[7]=8;
        myArray[8]=9;
        myArray[9]=10;
        int min = myArray[0]; for (int i = 0; i < myArray.length; i++) { 
            if (myArray[i] < min) min = myArray[i];
        }
       System.out.println(min);
    }
        }

Display array (print elements)

 
Displaying arrays: To print an array, you have to print each element in the array using a loop like the following:
……for (int i = 0; i < myList.length; i++) {
  System.out.print(myList[i] + " ");
}  ………..


Simplifying coding:

 Arrays can be used to greatly simplify coding for certain tasks. For example, suppose you wish to obtain the English name of a given month by its number. If the month names are stored in an array, the month name for a given month can be accessed simply via the index. The following code prompts the user to enter a month number and displays its month name:
String[] months = {"January", "February", ..., "December"};
System.out.print("Enter a month number (1 to 12): ");
int monthNumber = input.nextInt();
System.out.println("The month is " + months[monthNumber - 1]);
If you didn’t use the months array, you would have to determine the month name using a lengthy multi-way if-else statement as follows:
if (monthNumber == 1)
 System.out.println("The month is January");
else if (monthNumber == 2) 
System.out.println("The month is February"); ...
else  System.out.println("The month is December");
 
There are lots of ways you can use array with java programing in this article I used just few examples, you can learn more yourself using internet
Thank you friends



2 comments:

sairnenagle said...

NJ Gambling Commission Strikes Against Caesars Entertainment
The New Jersey Gambling 안산 출장안마 Commission is suing Caesars Entertainment, owner 강원도 출장샵 of a Las Vegas casino 서귀포 출장안마 that is 전라남도 출장샵 currently on the 김포 출장샵 Boardwalk.

smayslith said...

vw047 louis vuitton outlet ak861