Results 1 to 3 of 3
  1. #1
    mia69 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Question adding elements to a list

    Hi to all,

    here is a small part of my program

    Java Code:
     
     
       // for every attribute not mentioned in the rule  r
                enumAtt = ruleE.enumerateAttributes();
                while (enumAtt.hasMoreElements()) 
                {
     
                     Attribute attr = (Attribute) enumAtt.nextElement();
                     if (isMentionedIn(attr, r.m_test)) {continue;}
                     int M = attr.numValues();
                     for (int val = 0; val < M; val ++) // ... for each value of this attribute
                     {
    				 
    				   // create a new condition
    				          Test Tst3 =new Test() ;
                              Tst3.m_attr=attr.index();
                              Tst3.m_val=val;
    						  
                              PrismRule cr = addRule(null , new PrismRule(data, r.m_classification)); // create a new empty candidate rule cr
                              for(int l=0 ; l< r.testSet.size();l++){ cr.testSet.add( r.testSet.get(l));  } //add to this rule all conditions in rule r     
                              cr.testSet.add(Tst3); // add the new condition to the rule cr
                               // we cast the testSet which is a list to m_test which is type Test 
                              if(cr.testSet.size()==1) {  cr.m_test = cr.testSet.get(0);} 
                              else if (cr.testSet.size()>1 ) { 
                              cr.m_test = cr.testSet.get(0);
                              st g=cr.m_test;
                              for(int i=1; i<cr.testSet.size(); i++) { 
                              g.m_next =cr.testSet.get(i); 
                              g=g.m_next; 
     
                              }
                              } 
     
    System.out.println("regle added  ====  "+cr); // print this new cr
     CR.add(cr);  // add this new candidate rule to the list CR
     
     
     
     
                     }// end the for of the values
     
                } // end the for of attributes
     
     
                // i print the elements of the list CR "which are new cr created for each value of each attribute"
                for(int m=0;m<CR.size();m++){System.out.println("regle num ["+m+"]==== "+CR.get(m));}
     
      return  CR ;
       }
    the ligne


    Java Code:
    System.out.println("regle added  ====  "+cr);
    prints rule cr added to CR and here what it shows
    Java Code:
    regle added  ====  If length = small
       and color = Red then True
     
    regle added  ====  If length = small
       and color = Green then True
     
    regle added  ====  If length = small
       and color = Blue then True
     
    regle added  ====  If length = small
       and age = young then True
     
    regle added  ====  If length = small
       and age = old then True
     
    regle added  ====  If length = small
       and neighbors = 1 then True
     
    regle added  ====  If length = small
       and neighbors = 2-to-4 then True
     
    regle added  ====  If length = small
       and neighbors = 5-to-9 then True
     
    regle added  ====  If length = small
       and neighbors = more-than-10 then True
    which is what i want , and these rule are added the list CR
    now the ligne
    Java Code:
     for(int m=0;m<CR.size();m++){System.out.println("regle num ["+m+"]==== "+CR.get(m));}
    should print the rules showed before , instead this what it gives me :
    Java Code:
    regle num [0]==== If length = small
       and neighbors = more-than-10 then True
     
    regle num [1]==== If length = small
       and neighbors = more-than-10 then True
     
    regle num [2]==== If length = small
       and neighbors = more-than-10 then True
     
    regle num [3]==== If length = small
       and neighbors = more-than-10 then True
     
    regle num [4]==== If length = small
       and neighbors = more-than-10 then True
     
    regle num [5]==== If length = small
       and neighbors = more-than-10 then True
     
    regle num [6]==== If length = small
       and neighbors = more-than-10 then True
     
    regle num [7]==== If length = small
       and neighbors = more-than-10 then True
     
    regle num [8]==== If length = small
       and neighbors = more-than-10 then True
     
    les regle If length = small
       and neighbors = more-than-10 then True
    it repeats only the last rule , why is that !!!!!!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: adding elements to a list

    You might be able to find the bug yourself if you would format your code properly. The use of indentation, braces, and not putting code blocks on a single line would help. It will also get you help quicker on this forum.

    E.g. Do this
    Java Code:
    for(int l = 0; l <  r.testSet.size(); l++) {
        cr.testSet.add(r.testSet.get(l));
    }
    Not this:
    Java Code:
    for(int l=0 ; l< r.testSet.size();l++){ cr.testSet.add( r.testSet.get(l));  }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    mia69 is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default Re: adding elements to a list

    here is a code after some identation:
    Java Code:
    // for every attribute not mentioned in the rule  r
                enumAtt = ruleE.enumerateAttributes();
                while (enumAtt.hasMoreElements())  
                {
     
                     Attribute attr = (Attribute) enumAtt.nextElement();
                     if (isMentionedIn(attr, r.m_test)) {continue;} // if the attribute is already in rule r skip  it
                     int M = attr.numValues();
                     for (int val = 0; val < M; val ++) // ... for each value of this attribute
                     {
     
    				         // create a new condition of type Test
    				          Ts3=creatCondition(attr,val);
    						  PrismRule cr = addRule(null , new PrismRule(data, r.m_classification)); // create a new empty candidate rule cr
                              for(int l=0 ; l< r.testSet.size();l++){ 
                                  cr.testSet.add( r.testSet.get(l));  
                             } //add to this new condidate rule all conditions in rule r     
                              cr.testSet.add(Tst3); // add the new condition to set of conditions of the rule cr
                               // we cast the testSet which is a list to m_test which is type Test 
     
     
                               System.out.println("regle added  ====  "+cr); // print this new cr
                               CR.add(cr);  // add this new candidate rule to the list CR
     
                     }// end the for of the values
     
                } // end the while of attributes enumeration
     
     
                // i print the elements of the list CR "which are new cr created for each value of each attribute"
                for(int m=0;m<CR.size();m++){
                     System.out.println("regle num ["+m+"]==== "+CR.get(m));
                }
     
      return  CR ;
       }
    Last edited by mia69; 02-06-2014 at 04:47 PM.

Similar Threads

  1. Replies: 4
    Last Post: 05-21-2013, 09:21 AM
  2. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  3. Adding elements to an ArrayList
    By ArcherSam in forum Advanced Java
    Replies: 7
    Last Post: 01-28-2011, 03:05 PM
  4. Adding the Elements of a 2D Array
    By RMcLuckie45 in forum New To Java
    Replies: 0
    Last Post: 11-07-2010, 11:04 PM
  5. Adding elements in array list and vectors using threads
    By hina.yousuf@seecs.edu.pk in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2010, 03:07 AM

Posting Permissions

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