2011-10-07 4 views
1

숙제를하는 중이다.자바 기반 인쇄 위치

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

을하고 이러한 색상은 텍스트에서 발생합니다

의 내가 배열 색상이 있다고 가정 해 봅시다. 색상이 발생하면 다른 배열 (위치 배열)에 줄 번호를 저장하는 다른 배열이 있습니다.

orange 
green 
black 
red 
blue 

를 I합니다 (Arrays.sort에 사용)에 :

[17,4,5,8,8]

은 이제 출력 될 수 있도록 광고 상승이 발생하여 인쇄 할 위치 배열을 정렬하십시오. 나는 이것이 위치를 사용하여 이루어져야한다고 생각한다.

예를 들어 주황색 인쇄의 경우 배열 색상에 정렬 된 배열과 색상의 위치와의 관계가 있습니다.

어떤 방향으로 나를 가리킬 수 있습니까?

나는 이것을 가능한 한 가장 간단한 방법으로 수행하기 위해 자바를 배우기 시작했다.

답변

2
  1. 색인을 서로 연관시켜야합니다. 클래스 (아마도 Pair 클래스, 속성은 String colorint line)에서이 작업을 수행 할 것을 권장합니다. 한 쌍의 개체

    class Pair { 
        public String color; 
        public int line; 
        public Pair(String color, int line) { 
         this.color = color; 
         this.line = line; 
        } 
    } 
    
  2. 배열을 구축 (또는 List<Pair>). 사용자의 속성에 따라 line

    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. 정렬 Arrays.sort을 사용 Comparator (또는 Collection.sort)와 Pair의 배열 방법.

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

    또 다른 옵션은 PairComparable<Pair>을 구현하는 것입니다.

  4. 인쇄 루프이 저와 출력 작동

+0

이제 자바를 배우기 시작 했으므로 간단하게 완료해야합니다. 나는 4 단계를 수행하는 방법을 알고있다. – Favolas

+0

@Favolas : 해석에 도움이되는 몇 가지 코드를 추가했다. :) – dacwe

0

아마 SortedMap을 사용할 것입니다.

+0

그냥 그래서 이것은이수록 간단하게 수행해야합니다 이제 자바를 배우기 시작했다. – Favolas

0

사용하여 어레이 :

오렌지 - 4
녹색 - 5
블랙 - 레드 8
- 8
을 파랑 - 17

Exmaple 코드 :

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);   
     } 
    }  
} 
0

당신은 HashMap에 사용할 수 있습니다. 모든 색상에 대해 색상 이름이 인 Key가 있고 그 Key의 경우 정수 정렬 목록이 필요합니다. 그래서 제가 추천 할 것입니다

..

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

당신은 당신이 위치를 추가합니다.

즉, 오렌지색 목록을 가져 와서 그 목록에 1을 더하고 싶다는 의미입니다.

그래서 당신은 당신이 할 것 목록 ...

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

하고를 반복하려는 경우 다음

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

이제 자바를 배우기 시작 했으므로 간단하게 완료해야한다. 처음 게시물을 편집하여 – Favolas