在另一个中使用来自一个for循环的数组索引值

时间:2013-09-04 14:13:38

标签: java arrays

我正在尝试计算数组中存在唯一数字的次数,使用的索引数量取决于输入的元素数量。主要是操作除了1.第一个值没有被考虑。循环正在检查arrays.length -1所以数字3显示为1的计数,即使我输入3两次。我知道修复此问题的最佳方法是运行一个不使用arrays.length -1的循环,但是我无法将条目与旁边的条目进行比较,如if(a[i] == a[i + 1] && a[i] != 0),以查看是否存在超过一次出现的值。我认为最好的办法是将计数值存储在我的count方法中,并计算相应的数组值,然后在方法之外做一个for循环可能吗?我无法看到这样做的方式,因为我对java很新。我可以提供一些指导:)

import java.util.Scanner;


public class Prac_ExeOne 
{
        static int count  = 1;
        static int numberUsed = 0; // static its used to show its a class wide variable and there is only one copy.
        static int[] Array =  new int [50]; // the maximum elements inside the array that can be used is 10;
        int size;

              public int fillArray(int[] a)
         {
                 System.out.println("Enter up to " + a.length + " nonnegative numbers.");
                 System.out.println("Mark the end of the list with a negative number.");
                 Scanner keyboard = new Scanner(System.in);

                 int next;
                 int index = 0;
                 next = keyboard.nextInt();
                 while ((next >= 0) && (index < a.length ))  
                 {
                     numberUsed++;
                     a[index] = next;
                     index++;
                     // Print out each value of next
                     System.out.println(next);
                     next = keyboard.nextInt();
                     //System.out.println("Number of indexes used" + numberUsed);
                 }
                 keyboard.close(); // close the keyboard so it can't continue to be used.
                 return index;
        }


            public int[] sort(int[] arrays)
            {

                for(int i = 0;i < arrays.length -1 ;i++ )
                {

                    int store = 0;
                    // Move Larger Values to the right.
                    if (arrays[i + 1 ] < arrays[i])
                    {
                        store = arrays[i];
                        arrays[i] = arrays[i + 1];
                        arrays[i + 1] = store;
                    }
                    // Sort swapped smaller values to the left.
                        for(int j = i; j > 1; j--)
                        {
                           if (arrays[j] < arrays[j - 1])
                           {
                           store = arrays[j];
                           arrays[j] = arrays[j - 1];
                           arrays[j - 1] = store;
                           }
                      }
                }
                return arrays;

            }
            public void count(int[] a)
            {   

                //for each element in array go through if conditons.
                System.out.println("N " + "Count");
                for(int i = 0;i < a.length -1;i++)
                {   
                    if(a[i] == a[i + 1] && a[i] != 0)
                    {
                        count++;

                    }
                    if(a[i] != a[i+1])
                    {
                        count = 1;
                    }
                    if (a[i] !=  0)
                     {
                    System.out.println(a[i] + " " + count);
                     }
                }
            }

            public static void main(String[] args) 
            {
                Prac_ExeOne score = new Prac_ExeOne();
                score.fillArray(Array);
                score.sort(Array);
                score.count(Array);


            }
        }

输入:

Enter up to 50 nonnegative numbers.
Mark the end of the list with a negative number.
3
3
2
2
-2

输出:

N Count
3 1
2 2
2 1

期望的结果:

简而言之,我希望程序正确计算值,然后在N下显示左边的值,并在数组中显示它在Count右下方的次数

3 个答案:

答案 0 :(得分:1)

  

简而言之,我希望程序正确计算值

计入Map

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class App {

    public static void main(String[] args) throws Exception {
        List<Integer> ints = new ArrayList<>();
        ints.add(3);
        ints.add(3);
        ints.add(2);
        ints.add(2);
        ints.add(-2);
        ints.add(5);

        Map<Integer, Integer> counts = new HashMap<>();

        for (Integer i : ints) {
            if (i < 0) {
                break;
            }
            if (counts.containsKey(i)) {
                counts.put(i, counts.get(i) + 1);
            } else {
                counts.put(i, 1);
            }
        }

        System.out.println(counts);
    }
}

输出:

{2=2, 3=2}

答案 1 :(得分:1)

适用于sort功能

for (int j = i; j > 1; j--)

应该是

for (int j = i; j > 0; j--)

我在调用之后放System.out.println(Arrays.toString(Array));并在开头看到3,这导致我跳过了第一个元素。

请注意,还有way more efficient sorting algorithms

对于count功能,您在错误的时间重置count并经常打印。我改变如下:

public void count(int[] a)
{
    //for each element in array go through if conditions.
    System.out.println("N " + "Count");
    for (int i = 0; i < a.length - 1; i++)
    {   
        if (a[i] != 0)
        {
            if (a[i] == a[i + 1])
            {
                count++;
            }
            // if the next element is different, we already counted all of the
            //   current element, so print it, then reset the count
            else
            {
                System.out.println(a[i] + " " + count);
                count = 1;
            }
        }
    }
    // we haven't processed the last element yet, so do that
    if (a[a.length-1] != 0)
        System.out.println(a[a.length-1] + " " + count);
}

答案 2 :(得分:1)

如果你真的想使用带有唯一计数器的数组,你可以使用下面的代码:

public class Prac_ExeOne {
    static int count = 1;
    static int numberUsed = 0; // static its used to show its a class wide variable and there is only one copy.
    static Integer[] Array = new Integer[50]; // the maximum elements inside the array that can be used is 10;
    int size;

    public int fillArray(Integer[] a) {
        System.out.println("Enter up to " + a.length + " nonnegative numbers.");
        System.out.println("Mark the end of the list with a negative number.");
        Scanner keyboard = new Scanner(System.in);

        int next;
        int index = 0;
        next = keyboard.nextInt();
        while ((next >= 0) && (index < a.length)) {
            numberUsed++;
            a[index] = next;
            index++;
            // Print out each value of next
            System.out.println(next);
            next = keyboard.nextInt();
            // System.out.println("Number of indexes used" + numberUsed);
        }
        keyboard.close(); // close the keyboard so it can't continue to be used.
        return index;
    }

    public Integer[] sort(final Integer[] arrays) {
        Arrays.sort(arrays, new Comparator<Integer>() {

            @Override
            public int compare(Integer int1, Integer int2) {
                if (null != int1 && null != int2) {
                    if (int1 < int2) {
                        return -1;
                    } else if (int1 > int2) {
                        return 1;
                    } else  {
                        return 0;
                    }
                }
                return 0;
            }

        }); 
        return arrays;

    }

    public void count(Integer[] a) {

        // for each element in array go through if conditons.
        System.out.println("N " + "Count");
        for (int i = 0; i < a.length - 1; i++) {
            if (i == 0 && a[i] != null) {
                System.out.println(a[i] + " " + count);
            }
            if (i > 0 && (a[i] != null && a[i - 1] != null)) {
                if (a[i] == a[i - 1]) {
                    count++;    
                }
                if (a[i] != a[i - 1]) {
                    count = 1;
                }
                if (a[i] != 0) {
                    System.out.println(a[i] + " " + count);
                }
            } else {
                count = 1;
            }
        }
    }

    public static void main(String[] args) {
        Prac_ExeOne score = new Prac_ExeOne();
        score.fillArray(Array);
        score.sort(Array);
        score.count(Array);        
    }
}

结果:

输入最多50个非负数。

使用负数标记列表末尾。

3

3

2

2

-1

N计数

2 1

2 2

3 1

3 2

由于您的排序方法,您的代码无效。 (抱歉我的英语不好)