2016-05-02 25 views
-1

オブジェクト配列のリストがあります。オブジェクトの配列は、私はそれらの結果を取得する方法を知ることができるJava:オブジェクト配列のリストからレコードの最小および最大のオブジェクト配列を取得します。

result: 

["CCC", 29] - max age 
["DDD", 18] - min age 

、後述するように最小値と最大年齢レコードを取得する必要があります値「名前」&「年齢」

list 1 
object arrray 1 -- ["AAA", 28] 
list 2 
object arrray 2 -- ["BBB", 25] 
list 3 
object arrray 3 -- ["CCC", 29] 
list 4 
object arrray 4 -- ["DDD", 18] 
list 5 
object arrray 5 -- ["EEE", 20] 

を持って

+0

これまでにどのようなコードを試しましたか? –

+0

[宿題の助けを求める質問には、これまで問題を解決するために行われた作業の概要と問題の解決の説明が含まれている必要があるため、このトピックをトピックとして閉じようとしています(http:// stackoverflow .com/help/on-topic)。 – Andreas

答えて

0

使用Comparable

public class CompareAge implements Comparator<YourObject> { 
    @Override 
    public int compare(YourObject o1, YourObject o2) { 
     return o1.getAge().compareTo(o2.getAge()); 
    } 
} 

続いた後、ソートそれとretrieあなたは、内側の配列オブジェクトに到達するために、その後IndexOuterとIndexInnerを使用することができますObject

Collections.sort(myArrayList, new CompareAge()); 
    YourObject maxAgeObj = myArrayList.get(myArrayList.size() - 1); //Retrieve the last object 
    YourObject minAgeObj = myArrayList.get(0); 
+0

'Object'には' getAge'メソッドがありません。そして 'Comparator'は' interface 'ではありません。 –

+0

@ElliottFrisch私はそれがプロパティとして名前と年齢を持つPOJOだと仮定します。オブジェクトとして表現しました。 –

+0

OPはオブジェクトの配列のリストだと言っています。だから私はそれが安全な前提だとは思わない。 「オブジェクト配列」の番号付けに注意してください。 –

0
List objects; 

int max = Integer.MIN_VALUE; 
int min = Integer.MAX_VALUE; 
int maxIndexOuter = -1; 
int minIndexOuter = -1; 
int maxIndexInner = -1; 
int minIndexInner = -1; 

for(int i = 0 ; i < objects.length ; i++){ 
for(int x = 0 ; x < objects[i].length ; x++){ 
    if(objects[i][x].age > max){ 
    max = objects[i][x].age; 
    maxIndexOuter = i; 
    maxIndexInner = x; 
    } 
    if(objects[i][x].age < min){ 
    min = objects[i][x].age; 
    minIndexOuter = i; 
    minIndexInner = x; 
    } 
} 
} 
  • ArrayListから最後と最初のオブジェクトVEの。
関連する問題