Jonathan Lam
Jonathan Lam

Reputation: 17361

How to get a certain array element's key?

I'm pretty new to java, and I'm trying to create a simple method that sorts inputted numbers, either ascending or descending. However, there's a problem that I can't put in repeated values. Is there a way to get the key of a certain item of an array??

My code:

import java.io.Console;

public class TestSort {
    public static void main(String args[]) {
        Console c = System.console();
        if (c == null) {
            System.err.println("No console.");
            System.exit(1);
        }

        System.out.println("TESTSORT.java");
        System.out.println("-------------");
        System.out.println("Type in a set of numbers here:");
        String in = c.readLine();
        System.out.println("(A)scending or (D)escending");
        String ad = c.readLine();

        boolean d = false;

        if(ad.equals("a")) d = false;
        else if(ad.equals("d")) d = true;
        else {
            System.out.println("Invalid Input.");
            System.exit(1);
        }

        String[] in2 = in.split(" ");
        int[] x = new int[in2.length];

        int count1 = 0;
        for(String val : in2)
            x[count1++] = Integer.parseInt(val);

        int[] a = new int[x.length];
        int count = 0;

        for(int y : x) {
            for(int z : x) {
                // if index of y equals index of z continue
                if(z < y) count++;
            }
            a[count] = y;
            count = 0;
        }

        if(d) {
            int[] arr3 = new int[a.length];
            int length = a.length;
            for(int b : a) arr3[--length] = b;
            for(int b : arr3) System.out.println(b);
        } else
            for(int b : a)
                System.out.println(b);
    }
}

This program just counts up the number of other numbers smaller than itself, but not including itself. However, it doesn't differentiate itself from other numbers with the same value.

Help would be appreciated.

Thanks.

Upvotes: 0

Views: 136

Answers (3)

James Williams
James Williams

Reputation: 678

A SortedSet is perfect for this. As a set, it does not allow duplicate values, and it is sorted automatically for you!

Just add your elements to the set, e.g:

SortedSet<Integer> set = new SortedSet<Integer>();
for(String value : in2.split(" ")){
    set.add(Integer.parseInt(value));
}

To reverse the order of the set do something like this:

SortedSet<Integer> descending = set.descendingSet();

You can iterate through sets just like arrays too:

for(Integer i : set){
    //Do something
}

Good luck!

Upvotes: 0

sideshowbob
sideshowbob

Reputation: 316

If you want an index you should not be using for each loops. You will have to use a regular for loop to get at an index in the array.

Upvotes: 1

Java Devil
Java Devil

Reputation: 10959

To get the index of a certain value for an array you will have to loop through the array. However if there is multiple entries with the same value this approach wouldn't work (without modification)

 int indexVal = -1;
 int inputValue;  // This is your input vlaue you are trying to find
 for(int i = 0; i < array.length ; i++)
 {
     if (array[i] == inputValue)
     {
         indexVal = i;
         break;
     }
 }

You may also want to look at Array.sort for built in array sorrting

Upvotes: 3

Related Questions