简体   繁体   中英

How to make three arrays relate to each other

I have written a program which reads an input file and store the values in three arrays(a string and two double arrays). The class who do the task is given below:

package Assignments;

import java.util.Arrays;
import java.util.Scanner;


public class Unit {

static double[] fatContent;
static double[] protienContent;
static String[] nameOfElements;


Unit(int numberOfElements){
    fatContent=new double[numberOfElements];
    protienContent=new double[numberOfElements];
    nameOfElements=new String[numberOfElements];

}

static void start(String in,int counter){
     Scanner storingElements=new Scanner(in);
    double fat;
    double protien;
    String name;

    while(!storingElements.hasNext()){

        storingElements.nextLine();

    }
    name=storingElements.next();
    fat=storingElements.nextDouble();
    protien=storingElements.nextDouble();
    fatContent[counter]=fat;
    protienContent[counter]=protien;
    nameOfElements[counter]=name;

}


}

One of the input file I am reading is:

7
25
2
Mammal  fat_content(%)  protein_content(%)
Bison   7.9 5.9
Guinea_pig  3.9 8.1
Dolphin 14.0    10.4
Donkey  1.4 1.7
Goat    4.1 3.4
Deer    19.7    9.2
Dog         8.3 9.5
Yak     6.7 5.3
Camel   3.4 3.5
Cat     10.9    11.1
Rabbit  13.1    7.1
Llama   3.2 3.9
Human   43.9    7.4
Mule    1.8 2.0
Elephant    5.0 4.0
Horse   1.3 2.1
Rat     12.6    12.3
Reindeer    20.3    10.4
Sheep   6.4 5.6
Pig         5.1 6.6
Fox     5.9 7.4
Whale   42.3    10.9
Polar_bear  31.0    10.2
Zebra   4.8 3.0
Seal    53.2    11.2

and when I run this class from my main program it gives me output of:

[Bison, Guinea_pig, Dolphin, Donkey, Goat, Deer, Dog, Yak, Camel, Cat, Rabbit, Llama, Human,     Mule, Elephant, Horse, Rat, Reindeer, Sheep, Pig, Fox, Whale, Polar_bear, Zebra, Seal]

[7.9, 3.9, 14.0, 1.4, 4.1, 19.7, 8.3, 6.7, 3.4, 10.9, 13.1, 3.2, 43.9, 1.8, 5.0, 1.3, 12.6,     20.3, 6.4, 5.1, 5.9, 42.3, 31.0, 4.8, 53.2]

[5.9, 8.1, 10.4, 1.7, 3.4, 9.2, 9.5, 5.3, 3.5, 11.1, 7.1, 3.9, 7.4, 2.0, 4.0, 2.1, 12.3, 10.4, 5.6, 6.6, 7.4, 10.9, 10.2, 3.0, 11.2]

But my problem is that I want a connection between them like if I find the maximum value of fat array then it also gives me the corresponding value of protein and mammal name that belongs to it. I mean how can I make them relate to each other.... like (Bison 7.9 5.9) at one place.

Thanks in advance and any ideas or help would be highly appreciated.....

Create a new class that contains all the three values, and instead of having three arrays, have one array of that class.

For example:

public class MammalValue {
    private String name;
    private double fat;
    private double protein;

    // getters & setters here
}

Like this you can have an array: MammalValue[] values

What you want is a new datatype, which contains two doubles and one String. from this, you create one array.

You could do it starting messing with indexes, but that would make it more complex than it should be, and it would be more prone to errors.

You can create a POJO class with getter-setter of Mammal,fat_content(%),protein_content(%) fields.

and create array of these objects . So you get three values together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM