码迷,mamicode.com
首页 > 编程语言 > 详细

Java Array Exercises Practice Solution I

时间:2020-06-07 01:00:04      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:mysq   values   lang   temp   bcd   practice   db2   mysql   lazy   

1. Write a Java program to reverse an array of integer values.

技术图片

package com.w3resource;

import java.util.Arrays;

public class ReverseArray {
    
    public static void main(String[] args) {
        
        int[] my_array = {5, 2, 7, 9, 6};
        System.out.println("Original array: " + Arrays.toString(my_array));
        
        for (int i = 0; i < my_array.length / 2; i++) {

            int temp = my_array[i];
            my_array[i] = my_array[my_array.length - i - 1];
            my_array[my_array.length - i - 1] = temp;
        }
        System.out.println("Reverse array: " + Arrays.toString(my_array));
    }
}

2. Write a Java program to find the duplicate values of an array of integer values.

技术图片

package com.w3resource;

public class FindDuplicateElement {
    
    public static void main(String[] args) {
        
        int[] my_array = {5, 2, 7, 7, 5};
        
        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]) && (i != j)) {
                    
                    System.out.println("Duplicate Element: " + my_array[j]);
                }
            }
        }
    }
}

3. Write a Java program to find the duplicate values of an array of string values.

技术图片

package com.w3resource;

public class FindDuplicateString {
    
    public static void main(String[] args) {
        
        String[] my_array = {"bcd", "abd", "jude", "bcd", "oiu"};
        
        for (int i = 0; i < my_array.length - 1; i++) {
            
            for (int j = i + 1; j < my_array.length; j++) {
                
                if ( (my_array[i].equals(my_array[j])) && (i != j) ) {
                    
                    System.out.println("Duplicate Element is: " + my_array[j]);
                }
            }
        }
    }
}

4. Write a Java program to find the common elements between two arrays (string values).

技术图片

package com.w3resource;

import java.util.Arrays;
import java.util.HashSet;

public class FindCommonElement {
    
    public static void main(String[] args) {
        
        String[] my_array = {"Python", "JAVA", "PHP", "C#", "C++", "SQL"};
        String[] new_array = {"MySQL", "SQL", "SQLite", "Oracle", "PostgreSQL", "DB2", "JAVA"};
        
        System.out.println("my_array: " + Arrays.toString(my_array));
        System.out.println("new_array: " + Arrays.toString(new_array));
        
        HashSet<String> set = new HashSet<String>();
        
        for (int i = 0; i < my_array.length; i++) {
            
            for (int j = 0; j < new_array.length; j++) {
                
                if (my_array[i].equals(new_array[j])) {
                    set.add(my_array[i]);
                }
            }
        }
        
        System.out.println("Common Element is: " + set);
    }
}

Output

Array1 : [Python, JAVA, PHP, C#, C++, SQL]                                                                    
Array2 : [MySQL, SQL, SQLite, Oracle, PostgreSQL, DB2, JAVA]                                                  
Common Element is : [JAVA, SQL]

5. Write a Java program to find the common elements between two arrays of integers.

技术图片

package com.w3resource;

import java.util.Arrays;

public class CommonIntegerElement {
    
    public static void main(String[] args) {
        
        int[] my_array = {1, 2, 5};
        int[] new_array = {6, 5, 8};
        
        System.out.println("my_array: " + Arrays.toString(my_array));
        System.out.println("new_array: " + Arrays.toString(new_array));
        
        for (int i = 0; i < my_array.length; i++) {
            
            for (int j = 0; j < new_array.length; j++) {

                if (my_array[i] == new_array[j]) {
                    
                    System.out.println("Common Element is : " + array[i])
                }            
 	       }
    	}
    }
}

6. Write a Java program to remove duplicate elements from an array.

技术图片

package com.zetcode;

import java.util.Arrays;

public class RemoveDuplicateElements {

    static void unique_array(int[] my_array) {

        System.out.println("Original Array : ");

        for (int i = 0; i < my_array.length; i++) {
            System.out.print(my_array[i] + "\t");
        }

        //Assuming all elements in input array are unique

        int no_unique_elements = my_array.length;

        //Comparing each element with all other elements

        for (int i = 0; i < no_unique_elements; i++) {
            for (int j = i + 1; j < no_unique_elements; j++) {
                //If any two elements are found equal

                if (my_array[i] == my_array[j]) {
                    //Replace duplicate element with last unique element

                    my_array[j] = my_array[no_unique_elements - 1];

                    no_unique_elements--;

                    j--;
                }
            }
        }

        // Copying only unique elements of my_array into array1

        int[] new_array = Arrays.copyOf(my_array, no_unique_elements);

        // Printing array Without Duplicates

        System.out.println();

        System.out.println("Array with unique values : ");

        for (int i = 0; i < new_array.length; i++) {
            System.out.print(new_array[i] + "\t");
        }

        System.out.println();

        System.out.println("---------------------------");
    }

    public static void main(String[] args) {
        unique_array(new int[]{0, 3, -2, 4, 3, 2});

        unique_array(new int[]{10, 22, 10, 20, 11, 22});

    }
}

Output

Original Array : 
0	3	-2	4	3	2	
Array with unique values : 
0	3	-2	4	2	
---------------------------
Original Array : 
10	22	10	20	11	22	
Array with unique values : 
10	22	11	20	
---------------------------

7. Write a Java program to find the second largest element in an array.

技术图片

package com.zetcode;

import java.util.Arrays;

public class FindSecondLargest {

    public static void main(String[] args) {

        int[] my_array = {10789, 2035, 1899, 2046, 2049, 1456, 2013};

        System.out.println("Original numeric array: " + Arrays.toString(my_array));
        Arrays.sort(my_array);
        int index = my_array.length - 1;
        while (my_array[index] == my_array[my_array.length - 1]) {
            index--;
        }
        System.out.println("Second largest value: " + my_array[index]);
    }
}

8. Write a Java program to find the second smallest element in an array.

技术图片

package com.zetcode;

import java.util.Arrays;

public class FindSecondSmallest {

    public static void main(String[] args) {

        int[] my_array = {-1, 4, 0, 2, 7, -3};
        System.out.println("Original numeric array : " + Arrays.toString(my_array));
        int min = Integer.MAX_VALUE;
        int second_min = Integer.MAX_VALUE;
        for (int i = 0; i < my_array.length; i++) {
            if (my_array[i] == min) {
                second_min = min;
            } else if (my_array[i] < min) {
                second_min = min;
                min = my_array[i];
            } else if (my_array[i] < second_min) {
                second_min = my_array[i];
            }

        }
        System.out.println("Second lowest number is : " + second_min);
    }
}

Java Array Exercises Practice Solution I

标签:mysq   values   lang   temp   bcd   practice   db2   mysql   lazy   

原文地址:https://www.cnblogs.com/PrimerPlus/p/13057818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!