如何输出数组中元素的索引

时间:2018-12-16 01:06:00

标签: java arrays indexof

这是任务: 设计,实施和测试程序,以执行以下每个操作:

a)在10个整数列表中找到正整数的和和数量。

b)在10个整数列表中找到最小的数字。

c)确定并输出10个整数列表中的最大和最小数字。  输出应为以下形式: “最大的304位在列表中的第3位” “最小的数字4在列表中的位置8”

所以我尝试了一下,问题是除了给我最大和最小数字的位置以外,其他所有东西都起作用。

    import java.util.Arrays;

public class a5_2 {
    @SuppressWarnings("unlikely-arg-type")
    public static void main (String [] args) {


        int m [] = {-3,23,7,12,4,-44,2,21,3,43} ;

        System.out.println("Array: " + Arrays.toString(m));
        int[] pos = findNumber(m);
            System.out.println("Array without negatives: ");
                for (int i = 0; i < pos.length; i++) 
                    {
                        System.out.println(pos[i]);         
                    }


        System.out.println("Number of pos num: " + pos.length);
            int sum = 0;
                for (int i : pos)
                    sum += i;
        System.out.println("Sum of pos num: " + sum);


        int [] small = findSmallest(pos);
                System.out.println("Smallest Number: ");
                System.out.println(small[0] + " at pos: " + Arrays.asList(pos).indexOf(small[0]));

        int [] big = findBiggest(pos);
                System.out.println("Biggest Number: ");
                System.out.println(big[0] + " at pos: " + Arrays.asList(pos).indexOf(big[0]));
        }

        public static int [] findNumber(int[] sum) {
            int num = 0;
            int n [] = new int [sum.length];
                    for(int i = 0; i < sum.length; i++)
                    {
                        if (sum[i] > 0)
                        {
                            n[num] = sum[i];
                            num++;
                        }
                    }
            int [] pos =  new int [num];
                for (int k = 0 ; k < num ; k++)
                {
                    pos[k] = n[k];
                }

            return pos;

        }
    public static int [] findSmallest(int[] pos) {
        int temp;
        for (int i = 0; i < pos.length; i++)
        {
            for (int j = i + 1; j < pos.length; j++)
            {


                if (pos[i] > pos[j])
                    {
                        temp = pos[i];
                        pos[i] =pos[j];
                        pos[j] = temp;
                    }
            }
        }
        return pos;

    }
    public static int [] findBiggest(int[] pos) {
        int temp;
        for (int i = 0; i < pos.length; i++)
        {
            for (int j = i + 1; j < pos.length; j++)
            {


                if (pos[i] < pos[j])
                    {
                        temp = pos[i];
                        pos[i] =pos[j];
                        pos[j] = temp;
                    }
            }
        }
        return pos;

    }
} 

位置的输出为-1,而不是应提供的输出。

提前谢谢大家:)

2 个答案:

答案 0 :(得分:1)

Arrays.asList(pos)转换为List<int[]>并将其转换为List<Integer>然后获得索引

List<Integer> comArray = Arrays.stream(m).boxed().collect(Collectors.toList());

System.out.println(small[0] + " at pos: " + comArray.indexOf(small[0]));

在java-7中,我相信您已经知道如何将int[]转换为List<integer>

 List<Integer> comArray = new ArrayList<>();

        List<Integer> intList = new ArrayList<Integer>();
        for (int i : m)
        {
            intList.add(i);
        }

验证码

 @SuppressWarnings("unlikely-arg-type")
    public static void main (String [] args) {


        int m [] = {-3,23,7,12,4,-44,2,21,3,43} ;

        List<Integer> comArray = Arrays.stream(m).boxed().collect(Collectors.toList());

        System.out.println("Array: " + Arrays.toString(m));
        int[] pos = findNumber(m);
            System.out.println("Array without negatives: ");
                for (int i = 0; i < pos.length; i++) 
                    {
                        System.out.println(pos[i]);         
                    }


        System.out.println("Number of pos num: " + pos.length);
            int sum = 0;
                for (int i : pos)
                    sum += i;
        System.out.println("Sum of pos num: " + sum);


        int [] small = findSmallest(pos);
                System.out.println("Smallest Number: ");
                System.out.println(small[0] + " at pos: " + comArray.indexOf(small[0]));

        int [] big = findBiggest(pos);
                System.out.println("Biggest Number: ");
                System.out.println(big[0] + " at pos: " + comArray.indexOf(big[0]));
        }

        public static int [] findNumber(int[] sum) {
            int num = 0;
            int n [] = new int [sum.length];
                    for(int i = 0; i < sum.length; i++)
                    {
                        if (sum[i] > 0)
                        {
                            n[num] = sum[i];
                            num++;
                        }
                    }
            int [] pos =  new int [num];
                for (int k = 0 ; k < num ; k++)
                {
                    pos[k] = n[k];
                }

            return pos;

        }
    public static int [] findSmallest(int[] pos) {
        int temp;
        for (int i = 0; i < pos.length; i++)
        {
            for (int j = i + 1; j < pos.length; j++)
            {


                if (pos[i] > pos[j])
                    {
                        temp = pos[i];
                        pos[i] =pos[j];
                        pos[j] = temp;
                    }
            }
        }
        return pos;

    }
    public static int [] findBiggest(int[] pos) {
        int temp;
        for (int i = 0; i < pos.length; i++)
        {
            for (int j = i + 1; j < pos.length; j++)
            {


                if (pos[i] < pos[j])
                    {
                        temp = pos[i];
                        pos[i] =pos[j];
                        pos[j] = temp;
                    }
            }
        }
        return pos;

    }

 }

输出

Array: [-3, 23, 7, 12, 4, -44, 2, 21, 3, 43]
Array without negatives: 
23
7
12
4
2
21
3
43
Number of pos num: 8
Sum of pos num: 115
Smallest Number: 
2 at pos: 6
Biggest Number: 
43 at pos: 9

答案 1 :(得分:0)

因为Arrays.asList(pos)返回List<int[]>而不是List<Integer>