java - I need to make permutations with an array using the ArrayList Class. I can't figure out what's wrong -


so im taking ap comp sci class in school, , in class we're learning basics of java. assignment have make permutations taking numbers 1 one-dimensional array, , putting in another, deleting number can't picked again. numbers in array can't repeat. have use arraylist class too. , can't figure out what's wrong! method creates permutations:

 public static arraylist<integer> createperm()     {         arraylist<integer> list = new arraylist<integer>(10);         integer x = 1, remove = 0;         (integer = 0; < 10; i++)         {             list.add(x);             x++;         }          arraylist<integer> perm = new arraylist<integer>(10);          for(integer = 0; < 10; i++)          {             integer r = (int)(math.random() * 10) + 1;             (integer j = 0; j <= list.size() - 1; j++)             {                 if (list.get(j) == r)                 {                     remove = j + 1;                     list.remove(remove);                     perm.add(r);                 }             }         }          return perm; 

}

i think (and also:)) got lttle bit confused because using integer-objects index , list elements.
no problem list.get method, because there 1 method expecting int , java converts integer int.
problem in usage of list.remove(). there 2 methods, 1 expects object , 1 int.
if pass integer object, remove(object) method called. pass index, not r-matching object, remove method fails sometimes, because random if element in list if remove called before. , if method not fails, have removed element value of index(+1), not 1 matches r.

     for(integer = 0; < 10; i++)      {         integer r = (int)(math.random() * 10) + 1;         (integer j = 0; j <= list.size() - 1; j++)         {             if (list.get(j) == r)             {                 remove = j + 1;                 list.remove(remove);//the main error here found 4                                      //on index 2 , removes 3 (because of +1)                 perm.add(r);             }         }     } 

the next thing is, random can deliver same number more once,
should not loop 10 times. loop until list empty.
have corrected code below, original lines commented before correction.

    //for (integer = 0; < 10; i++) {     while (!list.isempty()) {         integer r = (int) (math.random() * 10) + 1;         (integer j = 0; j <= list.size() - 1; j++) {             //if (list.get(j) == r) {             if (list.get(j).equals(r)) {                 //remove = j + 1;                 remove = list.get(j);                 list.remove(remove);                 perm.add(r);             }         }     } 

and here put code more clearly, easier read

public static arraylist<integer> createperm() {     arraylist<integer> list = new arraylist<integer>(10);     (int = 0; < 10; i++) {         list.add(i+1);     }      arraylist<integer> perm = new arraylist<integer>(10);      while (!list.isempty()) {         int r = (int) (math.random() * 10) + 1;         (int j = 0; j < list.size(); j++) {             if (list.get(j).intvalue() == r) {                 perm.add(list.remove(j));             }         }     }      return perm; } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -