How to Output the Index of an Element in array

This was the task: Design, implement and test programs to do each of the following:

a) Find the sum and number of positive integers in a list of 10 integers.

b) Find the smallest number in a list of 10 integers.

c) Determine and output the biggest and smallest numbers in a list of 10 integers. The output should be of the form : “The biggest number 304 was at position 3 in the list” “The smallest number 4 was at position 8 in the list”

So I tried it and the Problem is that everything works except to give me the position of the biggest and smallest number.

    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;

    }
} 

Output of the position is -1 instead of the output it should give.

Thank you guys in advance :)

Upvotes: 0

Views: 265

Answers (2)

Ryuzaki L
Ryuzaki L

Reputation: 40048

Arrays.asList(pos) is converting into List<int[]> convert it into List<Integer> and then get the index

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

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

In java-7, i believe you know already how to convert int[] to List<integer>

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

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

Verified Code

 @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;

    }

 }

Output

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

Upvotes: 1

frank
frank

Reputation: 1201

because Arrays.asList(pos) return List<int[]> not List<Integer>.

Upvotes: 0

Related Questions