2016-01-05 38 views
2

所以我試圖調用我的方法,它包含我的數組的峯值列表。 但我正在從我的文本文件StringBuilder讀取數組作爲String。我應該做些什麼來調用該方法來執行該數組的功能,然後Toast結果或printf。請注意我的數組具有相等的空間1-D的字符串編號。列表到字符串。 java android

String Adata = String.valueOf(stringBuilder); 
// find the peaks 
String dataArray[] = Adata.split(" "); 
List aa = dataArray; 
List a = findPeaks(aa); // I can't convert to List. 
DataAlert alert = new DataAlert(); 
alert.show(getFragmentManager(), "DataAlert"); 
Peaks_value.setText(String.valueOf(a)); 
} 

    }); 

}//end OnCreate(). 

    public static List<Integer> findPeaks(List<Float> points) 
{ 
    ArrayList<Integer> peaks = new ArrayList<Integer>(); 
    float x1_n_ref = 0; 
    int alpha = 0; 

    for (int i = 0; i < points.size(); i++) { 
     if (x1_n_ref - points.get(i) < 0) { 
      x1_n_ref = points.get(i); 
      alpha = 1; 
     } else if (alpha == 1 && x1_n_ref - points.get(i) > 0) { 
      peaks.add(i); 
      alpha = 0; 
     } 
     x1_n_ref = 0; 

    } 


    return peaks; 
} 

回答

2

您需要正確的數據類型之間進行轉換,並確保您的泛型參數匹配:

  //I think using toString makes it easier to read 
      String Adata = stringBuilder.toString(); 
      String dataArray[] = Adata.split(" "); 

      //include generic parameter 
      List<Float> aa = new ArrayList<Float>(); 

      //Convert String[] into List<Float> 
      for(int i=0;i<dataArray.length;i++){ 
       //take String i from dataArray, convert it to a float with Float.parseFloat, then add it to your list 
       aa.add(Float.parseFloat(dataArray[i])); 
      } 

      //again, include the generic parameter 
      List<Integer> a = findPeaks(aa); 

      DataAlert alert = new DataAlert(); 
      alert.show(getFragmentManager(), "DataAlert"); 

      //again, using toString 
      Peaks_value.setText(a.toString()); 
     } 
    }); 
} 

public static List<Integer> findPeaks(List<Float> points) 
{ 
    ArrayList<Integer> peaks = new ArrayList<Integer>(); 
    float x1_n_ref = 0; 
    int alpha = 0; 

    for (int i = 0; i < points.size(); i++) { 
     if (x1_n_ref - points.get(i) < 0) { 
      x1_n_ref = points.get(i); 
      alpha = 1; 
     } else if (alpha == 1 && x1_n_ref - points.get(i) > 0) { 
      peaks.add(i); 
      alpha = 0; 
     } 
     x1_n_ref = 0; 

    } 


    return peaks; 
} 
+0

現在面臨着很附加兩次的問題,我檢索不一樣的數據。 –