简体   繁体   English

数组相互响应

[英]Arrays coresponding with 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. 我的意思是我该如何使它们彼此联系起来。...就像(Bison 7.9 5.9)在一个地方。

I have searched online and I got answers like making a new class and setting array to that class or some thing like that but I really don't understand that.....I would highly appreciate an answer with a little detail because I am new to programming. 我已经在网上搜索过,并且得到了诸如创建一个新类并为该类设置数组之类的答案,但是我真的不明白.....我非常感谢一个细节的答案,因为我编程新手。

Thanks in advance and any ideas or help would be highly appreciated..... 在此先感谢您,任何想法或帮助将不胜感激.....

You can create a dedicated class named "Mammal" which would have properties of fat content, name of mammal and protein content. 您可以创建一个名为“哺乳动物”的专用类,该类具有脂肪含量,哺乳动物名称和蛋白质含量的属性。 Make a array of Mammals and loop over to find the max fat content and easily you would find the corresponding protein content or any other query. 制作一系列哺乳动物并循环查找最大脂肪含量,您很容易就能找到相应的蛋白质含量或任何其他查询。

PS: You should have followed the object oriented paradigm. PS:您应该遵循面向对象的范例。 It's helpful. 有帮助

Here is the class, you need to improve it by yourself. 这是课程,您需要自己进行改进。 But the main idea is in it, as I understood it. 但据我了解,主要思想是其中。

class Mamal
{
    String Name;
    double fat;
    double protein;
    public Mamal(double fatContent,double proteinContent,String nameofElements)
    {
        Name=nameofElements;
        protein=proteinContent;
        fat=fatContent;
    }
    public String get_by_name(Mamal[] nameofElements,String name)
    {
        for(int i=0;i<nameofElements.length;i++)
        {
            if(nameofElements[i].getName().equalsIgnoreCase(name))
            {
                return nameofElements[i].getFat()+nameofElements[i].getName();
            }
        }
        return "";
    }
    public String getName() {
        return Name;
    }
    public double getFat() {
        return fat;
    }
    public double getProtein() {
        return protein;
    }
}

Function get_by_name helps you get the protein and fat of the mammal you need. 函数get_by_name可帮助您获取所需哺乳动物的蛋白质和脂肪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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