>
Arrays in Java: (Part 3)
One Dimensional Array (Accepting array and printing programs):
- Example: (Print static array (int) in Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Print a Static array in Java import java.util.Scanner; class test { public static void main(String ar[]) { int a[]={10,13,15,16,18}; // declare and initialize // length -> counts the number of values stored in array a for(int i=0 ; i<a.length ; i++) { System.out.println(i + " "+ a[i]); } } } /* OUTPUT 0 10 1 13 2 15 3 16 4 18 */ - Example: (Print Dynamic array (int) in Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Print a Dynamic array in Java import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); //take array size input from user System.out.println("Enter size of array:"); int size = scan.nextInt(); //take elements input from user int array[] = new int[size]; for(int i=0 ; i<array.length ; i++) // can also use size instead of length { System.out.println("Enter "+i+" element:"); // informing user to print which element array[i] = scan.nextInt(); //user enters element which is stored in array, at location i } // same loop to print the elements for(int i=0 ; i<array.length ; i++) // can also use size instead of length { System.out.println(i+" "+array[i]); } } } - Example: (Print static array (String) in Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Print a static (String) array in Java import java.util.Scanner; class test { public static void main(String ar[]) { String array[] = {"Hello","to","the","World","Of","Java"};// declare and initialize // length -> counts the number of values stored in array a for(int i=0 ; i<array.length ; i++) { System.out.println(i + " "+ array[i]); } } } /* OUTPUT 0 Hello 1 to 2 the 3 World 4 Of 5 Java */ - Example: (Print dynamic array (String) in Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Print a Dynamic array in Java import java.util.Scanner; class test { public static void main(String ar[]) { Scanner scan = new Scanner(System.in); //take array size input from user System.out.println("Enter size of array:"); int size = scan.nextInt(); scan.nextLine(); // clear the buffer after integer input. //take elements input from user String array[] = new String[size]; for(int i=0 ; i<array.length ; i++) // can also use size instead of length { System.out.println("Enter "+i+" element:"); // informing user to print which element array[i] = scan.nextLine(); //user enters sentences which are stored in array, at location i } // same loop to print the elements for(int i=0 ; i<array.length ; i++) // can also use size instead of length { System.out.println(i+" "+array[i]); } } } /* OUTPUT Enter size of array: 3 Enter 0 element: this is first Enter 1 element: this is second Enter 2 element: this is third 0 this is first 1 this is second 2 this is third */
Comments
Post a Comment