CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2014
    Posts
    1

    How to make three arrays relate to each other

    Hi guys,
    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:

    Code:
        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:

    Code:
        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:

    Code:
        [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.....

  2. #2
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: How to make three arrays relate to each other

    Use HashMap<String,Double[]> where the key is the element name and value would correspond to a Double array consisting of fat and protein counts. The only limitation in this approach is that you can't have 2 equal keys. And if you can't avoid it and do have more than 1 reg of the same element just use a Collection of Object arrays (for ex. ArrayList<Object[]>).
    Last edited by Xeel; June 25th, 2014 at 04:01 AM.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: How to make three arrays relate to each other

    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.
    Are you asking about "parallel" arrays? The connection between the arrays is the index. The contents of all the arrays at a specific index are related.
    Find the index of a desired value in one array and that index into the other arrays gives the corresponding values.
    Norm

  4. #4
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: How to make three arrays relate to each other

    I was going to suggest this from the beginning but the case of 2 or more equal numbers can create a problem that would remove the index uniqueness.

    Say we have 2 values which correspond to max of fat. Which index should I select? Map or Collection on the other hand solve this problem.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  5. #5
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: How to make three arrays relate to each other

    Good question for the OP:
    What if there are duplicate entries in one of the array?
    Norm

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured