开发者

Java print on based position

开发者 https://www.devze.com 2023-04-11 17:24 出处:网络
Doing homework and I\'m stuck. Let\'s say I have an array colors: [\"blue\", \"orange\", \"green\", \"black\", \"red\" ]

Doing homework and I'm stuck.

Let's say I have an array colors:

["blue", "orange", "green", "black", "red" ]

and these colors occur in a text. When the color occurs there is another array that stores the line number in another array (position array).

[17,4,5,8,8]

Now I want to print by ascending line occurrence so the output would be:

orange
green
black
red
blue

I use the Arrays.sort() to sort the position array. I believe that this should be d开发者_JAVA百科one using position.

For example, for printing orange, there is a relation of the sorted array with the position of the color on the array colors.

Can you point me in some direction?

As I started to learn java this as to be done the simplest way possible.


  1. You need to associate the indices with each other. I would recommend you to do it in a class (maybe a Pair class with the attributes String color and int line).

    class Pair {
        public String color;
        public int line;
        public Pair(String color, int line) {
            this.color = color;
            this.line  = line;
        }
    }
    
  2. Build up an array (or List<Pair>) of the paired objects.

    String[] colors = new String[] {"blue", "orange", "green", "black", "red"};
    int[] lines     = new int[]    {    17,        4,       5,       8,     8};
    
    List<Pair> pairs = new LinkedList<Pair>();
    for (int i = 0; i < colors.length; i++)
        pairs.add(new Pair(colors[i], lines[i]));
    
  3. Sort the array of Pairs with an Comparator using Arrays.sort (or Collection.sort) method depending on your line attribute.

    Collections.sort(pairs, new Comparator<Pair>() {
        public int compare(Pair p1, Pair p2) {
            return Integer.valueOf(p2.line).compareTo(p1.line);
        }
    });
    

    Another option would be to implement Comparable<Pair> in Pair.

  4. Print the array using a loop


I would probably use a SortedMap.


This works for me and outputs:

orange - 4
green - 5
black - 8
red - 8
blue - 17

Exmaple code:

public class Test
{  
    public String color;
    public int line;

    Test(String color, int line) {
        this.color = color;
        this.line = line;
    }

    public static void main(String[] args) 
    {
        String[] colors = new String[] { "blue", "orange", "green", "black", "red" };
        int[] lineNumber = new int[] { 17,4,5,8,8 };

        LinkedList<Test> out = new LinkedList<Test>();

        // add first element
        out.add(new Test(colors[0], lineNumber[0]));

        Loop: for(int i = 1; i < colors.length; i++) {
            for(int j = 0; j < out.size(); j++) {
                if(lineNumber[i] < out.get(j).line) {
                    out.add(j, new Test(colors[i], lineNumber[i]));
                    continue Loop;
                }
            }

            out.addLast(new Test(colors[i], lineNumber[i]));
        }

        for(Test t : out) {
            System.out.println(t.color + " - "+t.line);         
        }
    }      
}


You can use a HashMap. So for every color you have a Key by the color name and for that Key you will need some sort of List of integers.

So what I would recommend is..

Map<String color, List<Integer> location> hashMap = new HashMap<String Color, List<Integer> location>();

if you want to add a position you do.

hashMap.get("orange").add(1);

which means you want to get the list of orange and add the value 1 to that list.

So if you want to iterate that list you would do..

List<Integer> location = hashMap.get("orange"); returning the whole orange list

and then

for(Integer int : location)
{
   //Do whatever
}
0

精彩评论

暂无评论...
验证码 换一张
取 消