日期:2014-05-20  浏览次数:20688 次

如何使用Array储存两组数,并且确定这两组数相同?
使用Array储存数组1和数组2,并且使用类函数public static boolean equal(int[] list1, int[] list2)去确定两组数是否相同。
示例如下
Enter list1:
5 2 5 6 6 1

Enter list2:
5 5 2 6 1 6

The two lists are identical.


Enter list 1:
5 5 5 6 6 1

Enter list 2
5 2 5 6 1 6

The two lists are not identical
我写的代码如下无法运行
Java code
import java.util.Scanner;
public class identical{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        boolean[] equal=new boolean[];
        System.out.println("Enter list1:");
        int[] list1=new int[6];
        for (int i=0;i<list1.length;i++){
            list1[i]=input.nextInt();
        }
        System.out.print("Enter list2:");
        int[] list2=new int[6];
        for (int i=0;i<list2.length;i++){
            list2[i]=input.nextInt();
        }
        equal(list1[i],list2[i]);
        
    }
    public static boolean equal(int[] list1,int[] list2){
        boolean allEqual=true;
        for (int 1=0,i<equal.length,i++){
            list1[i]=list2[i];
            if (equal){
                allEqual=true;
                System.out.println("The two lists are identical.");
                return allEqual;
            
            }
            else {
                System.out.println("The two lists are not identical.");
                break;
            }
        }
    }
}


请教各位大神解答
还有一道题是Write a program called occurrences.java that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends. with 0. Make use of an array to keep track of the count.
示例如下
Enter the integers between 1 and 100
2 5 6 5 4 3 23 43 2 0

2 no of occurrences: 2
3 no of occurrences: 1 
4 no of occurrences: 1 
5 no of occurrences: 2
6 no of occurrences: 1
23 no of occurrences: 1
43 no of occurrences: 1
这道题完全没有想法,求解答,谢谢了

------解决方案--------------------
你这代码无语了,全是错。
给你一个能跑的。

Java code


import java.util.Arrays;
import java.util.Scanner;

public class identical {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter list1:");
        int[] list1 = new int[6];
        for (int i = 0; i < list1.length; i++) {
            list1[i] = input.nextInt();
        }
        System.out.println("Enter list2:");
        int[] list2 = new int[6];
        for (int i = 0; i < list2.length; i++) {
            list2[i] = input.nextInt();
        }
        Arrays.sort(list1);
        Arrays.sort(list2);
        equal(list1, list2);
    }

    public static void equal(int[] list1, int[] list2) {
        for (int i = 0; i < list1.length; i++) {
            if (list1[i] != list2[i]) {
                System.out.println("The two lists are not identical.");
                return;
            }
        }
        System.out.println("The two lists are identical.");
    }
}

------解决方案--------------------
第二道题,好好学学基础吧,顺便结个贴。。。

Java code

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class occurrences {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the integers between 1 and 100:");
        List<Integer> data = new ArrayList<Integer>();
        int num = input.nextInt();
        while (num != 0) {
            data.add(num);
            num = input.nextInt();
        }
        
        Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
        for (int key : data) {
            if (map.containsKey(key)) {
                map.put(key, map.get(key) + 1);
            } else {
                map.put(key, 1);
            }
        }
        
        for (int key : map.keySet()) {
            System.out.println( key + " no of occurrences: " + map.get(key));
        }
    }
}