2016-10-27 11 views
-3

ExArrayListを特定のgpaで検索したいですか? ArrayListにTrueまたはFalseの回答が必要な場合アイテムのArrayListを検索するにはどうすればよいですか?

public class ExArrayList { 
     private String Name; 
     private double GPA; 

     public ExArrayList(String name, double gpa) { 
      this.Name = name; 
      this.GPA = gpa; 

     } 
     public void setGPA(double gpa) { 
      this.GPA = gpa; 
     } 

     public double getGPA() { 
      return GPA; 
     } 
     public void setName(String name) { 
      this.Name = name; 
     } 
     public String getName() { 
      return Name; 
     } 
     @Override 
     public String toString() { 
      return String.format("%s\t%f", this.Name, this.GPA); 
     } 
    } 

    public class Main { 
     public static void main(String[] args) { 

      ArrayList<ExArrayList> psy101 = new ArrayList<>(); 
      psy101.add(new ExArrayList("Bob", 2.9)); 
      psy101.add(new ExArrayList("Steve", 3.9)); 
      psy101.add(new ExArrayList("Charles", 4.0)); 

      System.out.println(); 
      System.out.printf("Student\tGPA\n"); 
      for(ExArrayList s : psy101) { 
       System.out.printf("%s\n", s); 

      } 

      boolean binFound = psy101.contains(2.9); // This is what I am using to search the ArrayList. It's not working. 
      System.out.println("Does the list contain GPA of 2.9? " +  binFound);` 
+2

あなたが上書きする必要があり、その後の並べ替えやバイナリ検索 –

+0

[ 'ブール値のequals(オブジェクトo)を'](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html# equals-java.lang.Object-)および['int hashCode()'](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode-- ) '' ArrayList'から['' int indexOf(Object o) 'を使用したい場合(http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#indexOf-java) .lang.Object-)。 – Turing85

答えて

4

あなたはリストを蒸気との一致を探すために、ラムダを使用することができます:あなたはループごとに比較しなければならない

boolean binFound = psy101.stream().anyMatch(g -> g.getGPA() == 2.9); 
+0

これは 'Java 8'で動作します –

+0

@bradimusあなたの例は不要な自動ボックスを行い、' anyMatch() '引数は有効なラムダ式ではありません。 – Andreas

0

if(s.getGPA == 2.9){ 
     binFound = true; 
     break; 
    } 
Java8ストリームなし
+0

私はプログラミングには慣れていないと付け加えておきます。コメント/洞察を頂ければ幸いです。 – datorre

+1

これは 'Java'の倍精度を比較する正しい方法ではありません。 [this](http:// stackoverflowを参照してください。com/questions/179427/how-to-resolve-a-java-rounding-double-issue)と[this](http://stackoverflow.com/questions/8081827/how-to-compare-two-double-values -in-java) –

+1

このコードはコンパイルされません。 's.getGPA'はフィールドではなく、メソッドです。 – Turing85

0

:Java8ストリームに

boolean binFound = false; 
for(ExArrayList exArrayList : psy101) { 
     if(exArrayList.getGPA() == 2.9) { 
      binFound = true; 
      break; 
     } 
} 
System.out.println(binFound); 

boolean binFound = psy101.stream().map(ExArrayList::getGPA). 
anyMatch(gpa -> gpa == 2.9); 
System.out.println(binFound); 
-1

私はこれらの答えのいずれかを好きではなかったです。 doublesを比較しようとしたときに発生する精度エラーを忘れていました。 GPAの目的にはそれほど悪くはないはずですが、それには精度があまり関係しないからです。しかし、正しいやり方があります。 説明のためにthisおよびthisを参照してください。

あなたが計画していることを正しく実行する方法は、バイナリ検索です。しかし、まずリストをソートして機能させる必要があります。

Javaでオブジェクトを並べ替える方法は?最初に値を比較する方法を知っている必要があり、これを行うにはいくつかの方法があります。私の例では、Comparableを使用します。学習目的でthisをチェックしてください。

は何でも、あなたのオブジェクト内の

import java.util.*; 

今、私たちimplement Comparable前にこのインポートを使用してください。これは、変更後に次のようになります。

public class ExArrayList implements Comparable<ExArrayList> { 
    private String Name; 
    private double GPA; 

    public ExArrayList(String name, double gpa) { 
     this.Name = name; 
     this.GPA = gpa; 

    } 
    public void setGPA(double gpa) { 
     this.GPA = gpa; 
    } 

    public double getGPA() { 
     return GPA; 
    } 
    public void setName(String name) { 
     this.Name = name; 
    } 
    public String getName() { 
     return Name; 
    } 
    @Override 
    public String toString() { 
     return String.format("%s\t%f", this.Name, this.GPA); 
    } 
    //new stuff needed to compare the objects 
    public int compareTo(ExArrayList other) {     
     return Double.compare(this.GPA, other.GPA);   
    } 
} 

は、今、私たちは実行して、リストを並べ替えることができます。

Collections.sort(psy101); 

それをソートした後、我々は行うことによって、オブジェクトのインデックスを検索することができます

//here we must pass a fake object with the value that we are trying to find 
int index = Collections.binarySearch(psy101, new ExArrayList(null, 2.9)); 

System.out.println(index >= 0 ? "True" : "False"); 

indexは、リスト内の2.9の位置が0であり、見つからない場合は、負の数を含みます。

+0

downvoteを計画している場合は、まずコメントをしてください –

関連する問題