Trouver le Premier Quartile et le Troisième Quartile dans un tableau entier en utilisant java
public class tryA {
public static void main(String[] args) {
int[] intArray= new int[41];
System.out.println(intArray[intArray.length/2]);
}
Comment trouver le Quartile inférieur (Q1) et le Troisième Quartile (Q3) pour mon intArray de tableau entier? À condition que la taille du tableau puisse être une variable.
Ps: Il est utilisé pour trouver la valeur aberrante du tableau.
1
Author: Aatish Sai, 2017-02-22
1 answers
Je crois que c'est ce que vous recherchez, changez la variable quartile
en haut du code pour changer entre Q1, Q2, Q3 et 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;
}
}
0
Author: dat3450, 2017-02-22 06:35:17