2017-02-22 131 views
1
public class tryA { 

    public static void main(String[] args) { 
    int[] intArray= new int[41]; 
    System.out.println(intArray[intArray.length/2]); 

} 

整数配列ArrayArrayの下位四分位(Q1)と3分位(Q3)を見つけるにはどうすればよいですか?配列のサイズが変数である可能性があることを指定します。整数配列の最初の四分位数と第3四分位数をJavaを使用して検索する

P.S:アレイの異常値を見つけるために使用されます。

+0

どの方法を使用しますか?あなたは私達にこれを伝える必要があります。 –

+0

私はdouble LQ =(intArray [(median + 1)/ 2] + intArray [(median-1)/ 2])/ 2.0を使って最初の四分位点を見つけようとしました。常に有効ではないようです –

答えて

0

私はこれがあなたが探していると信じています。コードの先頭にあるquartile変数をQ1、Q2、Q3、Q4の間で変更してください。

import java.util.Arrays; 

public class ArrayTest 
{ 
    public static void main(String[] args) 
    { 
     //Specify quartile here (1, 2, 3 or 4 for 25%, 50%, 75% or 100% respectively). 
     int quartile = 1; 

     //Specify initial array size. 
     int initArraySize = 41; 

     //Create the initial array, populate it and print its contents. 
     int[] initArray = new int[initArraySize]; 
     System.out.println("initArray.length: " + initArray.length); 
     for (int i=0; i<initArray.length; i++) 
     { 
      initArray[i] = i; 
      System.out.println("initArray[" + i + "]: " + initArray[i]); 
     } 

     System.out.println("----------"); 

     //Check if the quartile specified is valid (1, 2, 3 or 4). 
     if (quartile >= 1 && quartile <= 4) 
     { 
      //Call the method to get the new array based on the quartile specified. 
      int[] newArray = getNewArray(initArray, quartile); 
      //Print the contents of the new array. 
      System.out.println("newArray.length: " + newArray.length); 
      for (int i=0; i<newArray.length; i++) 
      { 
       System.out.println("newArray[" + i + "]: " + newArray[i]); 
      } 
     } 
     else 
     { 
      System.out.println("Quartile specified not valid."); 
     } 
    } 

    public static int[] getNewArray(int[] array, float quartileType) 
    { 
     //Calculate the size of the new array based on the quartile specified. 
     int newArraySize = (int)((array.length)*(quartileType*25/100)); 
     //Copy only the number of rows that will fit the new array. 
     int[] newArray = Arrays.copyOf(array, newArraySize); 
     return newArray; 
    } 
} 
関連する問題