Skip to main content

Arrays in Java (Part 4)

>

Arrays in Java: (Part 4)

One Dimensional Array :

1. Sum all elements of integer array
2. Find maximum and minimum element
3. Find the longest string (largest length string)

  • Example: (Sum all elements of integer array)
    // Sum all elements of array in Java
    import java.util.Scanner;
    class test
    {
    public static void main(String ar[])
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the size of array:");
    int size = scan.nextInt();
    int array[] = new int[size];
    for(int i=0;i<array.length;i++)
    {
    System.out.println("Enter "+i+" element:");
    array[i] = scan.nextInt();
    }
    //To sum: As zero is additive constant, initialize a variable ans to 0 and keep on adding all the array values to this ans variable.
    int ans=0;
    for(int i=0;i<array.length;i++)
    {
    ans = ans+array[i];
    }
    System.out.println("Sum is: "+ans);
    }
    }
    /* OUTPUT
    Enter the size of array:
    5
    Enter 0 element:
    10
    Enter 1 element:
    20
    Enter 2 element:
    30
    Enter 3 element:
    40
    Enter 4 element:
    50
    Sum is: 150
    */
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (Find maximum and minimum element from integer array)
    // find the highest and lowest temperature recorded for a cities in Java
    // OR find max and min from array of N elements
    import java.util.Scanner;
    class test
    {
    public static void main(String ar[])
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the size of array:");
    int size = scan.nextInt();
    int array[] = new int[size];
    for(int i=0;i<array.length;i++)
    {
    System.out.println("Enter temperature for city no: "+ i);
    array[i] = scan.nextInt();
    }
    //To find minimum of all the array elements
    int min=array[0];
    for(int i=0;i<array.length;i++)
    {
    if(array[i]<min) // if you find an element in array that is less then min, shift that element as min.
    {
    min=array[i];
    }
    }
    System.out.println("Minimum Temperature is: "+min);
    //To find maximum of all the array elements
    int max=array[0];
    for(int i=0;i<array.length;i++)
    {
    if(array[i]>max) // if you find an element in array that is larger then max, shift that element as max.
    {
    max=array[i];
    }
    }
    System.out.println("Maximum Temperature is: "+max);
    }
    }
    /* OUTPUT:
    Enter the size of array:
    5
    Enter temperature for city no: 0
    50
    Enter temperature for city no: 1
    -7
    Enter temperature for city no: 2
    43
    Enter temperature for city no: 3
    5
    Enter temperature for city no: 4
    56
    Minimum Temperature is: -7
    Maximum Temperature is: 56
    */
    view raw pattern.java hosted with ❤ by GitHub
  • Example: (Find longest string in a string array in java)
    // find the longest string and its lenght from a string array in java
    import java.util.Scanner;
    class test
    {
    public static void main(String ar[])
    {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the size of array:");
    int size = scan.nextInt();
    String array[] = new String[size];
    scan.nextLine(); // clear the buffer after integer input
    for(int i=0;i<array.length;i++)
    {
    System.out.println("Enter "+i+" string");
    array[i] = scan.nextLine();
    }
    int max_length=array[0].length();
    int index=0;
    for(int i=0;i<array.length;i++)
    {
    if(array[i].length()>max_length) // if you find an element in array that is less then min, shift that element as min.
    {
    max_length=array[i].length();
    index=i; // backing up the index of the longest length string
    }
    }
    System.out.println("Highest Length: "+max_length);
    System.out.println("The element is: "+array[index]); // use index to print that element.
    }
    }
    /* OUTPUT
    Enter the size of array:
    5
    Enter 0 string
    blah
    Enter 1 string
    blah blah blah
    Enter 2 string
    blah blah
    Enter 3 string
    blah blah blah blah
    Enter 4 string
    blah blah blah blah hahahaha
    Highest Length: 28
    The element is: blah blah blah blah hahahaha
    */
    view raw pattern.java hosted with ❤ by GitHub

Comments

Post a Comment

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...

Characteristics of Java OR Java Buzzwords (Part 2)

> Java Properties: (Part 2) Robust: Java is robust because it is more reliable. It ensures the reliability by providing early checking for possible errors. It eliminates error causing constructs such as pointers. Java restricts the programmers in a few key areas, to force them to find mistakes early in program development. At the same time, Java frees from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks the code at compile time. However, it also checks your code at run time. Consider two main reasons of program failure: Memory management mistakes and Mishandled exceptional conditions (run time errors). Memory management is difficult and tedious in c/c++, the programmers have to manually allocate and free memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or, worse, try to ...

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