Skip to main content

Arrays in Java (Part 3)

>

Arrays in Java: (Part 3)

One Dimensional Array (Accepting array and printing programs):

  • Example: (Print static array (int) in Java)
    // 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 */
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (Print Dynamic array (int) in Java)
    // 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]);
    }
    }
    }
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (Print static array (String) in Java)
    // 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
    */
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (Print dynamic array (String) in Java)
    // 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
    */
    view raw test.java hosted with ❤ by GitHub

Comments

Relevant to YOU:

Popular posts from this blog

ASCII Code in Java (Part 1)

> The ASCII Code: (Part 1) ASCII stands for "American Standard Code for Information Interchange". As you may remember (Grade 9), computers only work with HIGH(1) and LOW(0) electrical states, known as bits, with correspond to mathematical Base 2 numbers. That is, computers only understand binary language. ASCII codes represent text (or other things) in computers. Assume that you are working with MS Word, or PPT or any other tool that uses text based inputs from user. You need to type a sentence that computer is not aware of. ASCII codes help to exchange this information between user and computer. When you type a character, it is converted into ASCII code first and then into Binary, which makes the computer understand what is to be typed. Hence every key on the keyboard has a specific ASCII code which is used to make the computer understand our language. If you press 4 from keyboard, then keyboard send the value 100 (value equival...

Java's Magic (Byte codes)

> What is JVM? What are Bytecodes (Java’s Magic): JVM is an abbreviation for Java Virtual Machine, which is a set of tools used to execute Java programs. JVM serves as the intermediary between your program and the host computer. Running a java program is a two-step process: With the compiler, first you translate a program into an intermediate language called Java bytecodes—the platform-independent codes interpreted by the interpreter on the Java platform. The interpreter parses and runs each Java bytecode instruction on the computer. Compilation happens just once; interpretation occurs each time the program is executed. Figure: Programs written in the Java programming language are first compiled and then interpreted. Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM). That is, in its standard form, the JVM is an interpreter for bytecode. Java bytecodes...

Character Class in Java (Part 2)

> The Character Class: (Part 1) Methods of Character class: Example: Method -> isDigit() [validating name] Example: Method -> isLetter() [validating mobile number] Example: Method -> isLetterOrDigit() [validating user_id / username] Example: Method -> isLowerCase() Example: Method -> isUpperCase() Example: Method -> toUpperCase() and toLowerCase() Click HERE for the list of Methods of Character class. Click HERE for the ASCII Table for different Characters