zoukankan      html  css  js  c++  java
  • Practice questions on Array

    Level I

    1. Take 10 integer inputs from user and store them in an array and print them on screen.

    package com.codesdope;
    
    import java.util.Scanner;
    
    public class StoreInputAndPrint {
    
        public static void main(String[] args) {
    
            Scanner s = new Scanner(System.in);
            int[] my_array = new int[10];
    
            for (int i = 0; i < my_array.length; i++) {
                System.out.println("Print the value of my_array[" + i + "]");
                my_array[i] = s.nextInt();
            }
    
            for (int i = 0; i < my_array.length; i++) {
                System.out.println("The value of my_array[" + i + "] is " + my_array[i]); 
            }
        }
    }
    

    2. Take 10 integer inputs from user and store them in an array. Now, copy all the elements in an another array but in reverse order.

    package com.codesdope;
    
    import java.util.Scanner;
    
    public class CopyElementsReverseOrder {
    
        public static void main(String[] args) {
    
            Scanner s = new Scanner(System.in);
            int[] my_array = new int[10];
            int[] new_array = new int[10];
    
            for (int i = 0; i < my_array.length; i++) {
                System.out.println("Enter the value of my_array[" + i + "]");
                my_array[i] = s.nextInt();
            }
    
            int j = 0;
            for (int i = new_array.length - 1; i>= 0; i--) {
                new_array[i] = my_array[j];
                j++;
            }
    
            for (int i = 0; i < new_array.length; i++) {
                System.out.println("The value of new_array[" + i + "] is " + new_array[i]);
            }
        }
    }
    

    3. Take 20 integer inputs from user and print the following:

    number of positive numbers

    number of negative numbers

    number of odd numbers

    number of even numbers

    number of 0s.

    package com.codesdope;
    
    import java.util.Scanner;
    
    public class GroupPrintNumbers {
    
        public static void main(String[] args) {
    
            Scanner s = new Scanner(System.in);
            int[] my_array = new int[20];
            int pos = 0;
            int neg = 0;
            int odd = 0;
            int even = 0;
            int zero = 0;
    
            for (int i = 0; i < my_array.length; i++) {
                System.out.println("Print the value my_array[" + i + "]");
                my_array[i] = s.nextInt();
    
                if (my_array[i] > 0) {
                    pos++;
                } else if (my_array[i] < 0) {
                    neg++;
                } else {
                    zero++;
                }
    
                if (my_array[i] % 2 == 0) {
                    even++;
                } else {
                    odd++;
                }
            }
            System.out.println("Positive : " + pos + "
    Negative : " + neg + "
    Zero : " + zero + "
    odd : " + odd + "
    even : " + even);
        }
    }
    

    4. Find largest and smallest of an array.

    package com.codesdope;
    
    import java.util.Scanner;
    
    public class LargestAndSmallest {
    
        public static void main(String[] args) {
    
            Scanner s = new Scanner(System.in);
            int[] my_array = new int[10];
    
            for (int i = 0; i < my_array.length; i++) {
                System.out.println("Enter the value of my_array[" + i + "]");
                my_array[i] = s.nextInt();
            }
    
            int largest = my_array[0];
            int smallest = my_array[0];
    
            for (int i = 0; i < my_array.length; i++) {
                if (my_array[i] > largest)
                    largest = my_array[i];
                if (my_array[i] < smallest)
                    smallest = my_array[i];
            }
    
            System.out.println("Largest is " + largest + " and smallest is " + smallest);
        }
    }
    

    5. Write a program to check if elements of an array are same or not it read from front or back.

    package com.codesdope;
    
    public class CheckElements {
    
        public static void main(String[] args) {
    
            int[] my_array = {2, 3, 15, 15, 3, 2};
            boolean read = true;
            int j = my_array.length - 1;
    
            for (int i = 0; i < my_array.length / 2; i++) {
                if (my_array[i] != my_array[j]) {
                    read = false;
                    break;
                } else {
                    j--;
                }
            }
            System.out.println(read);
        }
    }
    

    Level II

    1. Input any number. Find the sum of the digits of the number using a recursive function.

    package com.codesdope;
    
    import java.util.Scanner;
    
    public class RecursiveDigits {
        
        public static int sum(int num) {
            
            if ((num / 10) == 0) {
                return num;
            } else {
                return sum(num / 10) + (num % 10);
            }
        }
        
        public static void main(String[] args) {
            
            Scanner s = new Scanner(System.in);
            
            int number = s.nextInt();
            System.out.println(sum(number));
        }
    }
    

    2. Sorting refers to arranging data in a particular format. Sort an array of integers in ascending order. One of the algorithm is selection sort. Use below explanation of selection sort to do this.

    INITIAL ARRAY :

    2 3 1 45 15

    First iteration : Compare every element after first element with first element and if it is larger then swap. In first iteration, 2 is larger than 1. So, swap it.

    1 3 2 45 15

    Second iteration : Compare every element after second element with second element and if it is larger then swap. In second iteration, 3 is larger than 2. So, swap it.

    1 2 3 45 15

    Third iteration : Nothing will swap as 3 is smaller than every element after it.

    1 2 3 15 45

    Fourth iteration : Compare every element after fourth element with fourth element and if it is larger then swap. In fourth iteration, 45 is larger

    package com.codesdope;
    
    public class SelectSort {
        
        public static void main(String[] args) {
            
            int[] my_array = { 2, 3, 1, 45, 15 };
            int temp = 0;
            
            for (int i = 0; i < my_array.length - 1; i++) {
                for (int j = i + 1; j < my_array.length; j++) {
                    if (my_array[i] > my_array[j]) {
                        
                        temp = my_array[i];
                        my_array[i] = my_array[j];
                        my_array[j] = temp;
                    }
                }
            }
            
            for (int i = 0; i < my_array.length; i++) {
                System.out.println(my_array[i]);
            }
        }
    }
    

    3. Write a program to shift every elements of an array to circularly right.

    INPUT:

    1 2 3 4 5

    OUTPUT:

    5 1 2 3 4

    package codesdope;
    
    public class ShiftEveryElements {
        
        public static void main(String[] args) {
            
            int[] my_array = { 1, 2, 3, 4, 5 };
            int number = my_array[my_array.length - 1];
            
            for (int i = my_array.length - 1; i >= 1; i--) {
                my_array[i] = my_array[i - 1];
            }
            my_array[0] = number;
            for (int i = 0; i < my_array.length; i++) {
                System.out.println(my_array[i]);
            }
        }
    }
    

    4. Take an array of length n where all the numbers are nonnegative and unique. Find the element in the array possessing the highest value. Split the element into two parts where first part contains the next highest value in the array and second part hold the required additive entity to get the highest value. Print the array where the highest value get split into those two parts.

    取长度为n的数组,其中所有数字均为非负且唯一。在数组中找到具有最高值的元素。将元素分为两部分,第一部分包含数组中的下一个最大值,第二部分包含所需的加法实体以获取最大值。打印将最大值分成两个部分的数组。

    Simple input:4 8 6 3 2

    Simple output:4 6 2 6 3 2

    package com.codesdope;
    
    public class SplitHighestValue {
        public static void main(String[] args) {
            int[] my_array = {4, 8, 6, 3, 2};
            int[] new_array = new int[my_array.length + 1];
            int highest = my_array[0];
            int second_highest = my_array[0];
            int j = 0;
            for (int i = 0; i < my_array.length; i++) {
                if (my_array[i] > highest) {
                    highest = my_array[i];
                    j = i;
                }
            }
    
            for (int i = 0; i < my_array.length; i++) {
                if (my_array[i] < highest && my_array[i] > second_highest) {
                    second_highest = my_array[i];
                }
            }
    
            for (int i = 0; i < j; i++) {
                new_array[i] = my_array[i];
            }
    
            new_array[j] = second_highest;
            new_array[j+1] = highest - second_highest;
            for(int i = j + 2; i < new_array.length; i++) {
                new_array[i] = my_array[i - 1];
            }
            for (int i = 0; i < new_array.length; i++) {
                System.out.println(new_array[i]);
            }
        }
    }
    
  • 相关阅读:
    vector族函数
    (2)apply函数及其源码
    Eclipse如何修改默认工作空间路径
    怎么将码云的项目导入到eclipse
    在cmd中使用vim编译器
    win8中让cmd.exe始终以管理员身份运行
    cmd命令配置MySQL
    conda的一些指令
    今天遇到的“OS ERROR, permission denied” 如何解决的
    linux的环境变量
  • 原文地址:https://www.cnblogs.com/PrimerPlus/p/13037793.html
Copyright © 2011-2022 走看看