trouble with method algorythm
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: trouble with method algorythm

  1. #1
    Join Date
    Jan 2012
    Posts
    4

    trouble with method algorythm

    Hi Guys,

    Hoping someone can see where I messed up. I've created the test class below to which the output is:

    id: 40
    HID : 1
    NAME : sports
    id: 40
    HID : 4
    NAME : sewing
    temp.size(): 2
    HID/4
    NAME/sewing

    The final output should be:

    HID/1
    NAME/sports
    HID/4
    NAME/sewing

    This has me puzzled. temp.size() should be 4 NOT 2. It is clear from the println statements that my nested for loop in method getArrayFieldValues(...) is finding id 40 twice and putting the necessary values into the LinkedHashMap. But by the time it is done, it would seem the first two entries into the hashmap get overwritten or something leaving only the last two entries. I think this is one of those "can't see the forest for the trees" things. I need an extra pair of eyes to show me where I went wrong. Please advise.

    Alan

    Code:
    import java.util.*;   
      
    public class test   
    {   
      Field[] fields = null;   
         
      public static void main(String[] args)   
      {   
         test t = new test();   
         t.init();   
      }   
         
      private void init()   
      {   
         fields = new Field[2];   
         fields[0] = new Field("HID");   
         fields[1] = new Field("NAME");   
         
        String[][] data = new String[][]   
        {{"40","1","sports"},   
         {"41","1","sports"},   
         {"44","1","sports"},   
         {"39","2","crafts"},   
         {"38","3","photography"},   
         {"40","4","sewing"},   
         {"43","4","sewing"},   
         {"41","5","models"},   
         {"43","6","reading"}};   
            
         ArrayList<LinkedHashMap<String,Object>> hobbies = getArrayFieldValues("40",data);   
         LinkedHashMap<String,Object> temp = null;   
         for(int i = 0; i < hobbies.size(); i++)   
         {   
           temp = hobbies.get(i);   
           for (Map.Entry<String, Object> entry : temp.entrySet())   
           {   
             System.out.println(entry.getKey() + "/" + entry.getValue());   
           }   
              
         }      
      }   
         
      private ArrayList<LinkedHashMap<String,Object>> getArrayFieldValues(String id, String[][] data)   
      {   
             
          ArrayList<LinkedHashMap<String,Object>> list = new ArrayList<LinkedHashMap<String,Object>>();   
          int index = 0;   
          LinkedHashMap<String,Object> temp = new LinkedHashMap<String,Object>();   
          for(int i = 0; i < data.length; i++)   
          {   
              if(id.equals(data[i][0]))   
              {        
                  System.out.println("id: " + id);           
                  for(int j = 1; j < data[0].length; j++)   
                  {   
                    System.out.println(fields[index].getName() + " : " + data[i][j]);   
                    temp.put(fields[index].getName(),data[i][j]);   
                    index++;                   
                  }   
                  index = 0;//reset   
                     
              }   
          }   
          System.out.println("temp.size(): " + temp.size());   
          list.add(temp);   
          return list;   
      }   
    }   
      
    public class Field   
    {   
      private String name = "";   
      private String value = "";   
      
      public Field(String n)   
      {   
        name = n;   
      }   
      public Field(String n, String v)   
      {   
        name = n;   
        value = v;   
      }   
      
      public void setName(String value)   
      {   
        name = value;   
      }   
      
      public String getName()   
      {   
        return name;   
      }   
      
      public void setValue(String v)   
      {   
        value = v;   
      }   
      
      public String getValue()   
      {   
        return value;   
      }   
      
         
    }

  2. #2
    Join Date
    Jan 2012
    Posts
    4

    Re: trouble with method algorythm

    Problem solved. Had to rewrite method getArrayFieldValues(....):

    Code:
    private ArrayList<LinkedHashMap<String,Object>> getArrayFieldValues(String id, String[][] data)
      {
    	  
    	  ArrayList<LinkedHashMap<String,Object>> list = new ArrayList<LinkedHashMap<String,Object>>();
    	  int index = 0;
    	  LinkedHashMap<String,Object> temp = null; 
    	  for(int i = 0; i < data.length; i++)
    	  {
    		  if(id.equals(data[i][0]))
    		  {	
                                                temp = new LinkedHashMap<String,Object>();		  
                  		  
    			  for(int j = 1; j < data[0].length; j++)
    			  {
    			    temp.put(fields[index].getName(),data[i][j]);
    			    index++;			    
    			  }
    			  index = 0;//reset
    			  list.add(temp);
    		  }
    	  }
    	  	  
    	  return list;
      }
    Last edited by ashiers; June 5th, 2013 at 04:26 PM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center