How to Input a Nested For Loop output to ArrayList Dynamically-Android

by vinay sharma   Last Updated August 01, 2020 11:26 AM - source

I am a beginner to Android and I have written a code which find outs prime numbers between two numbers(x & y).

I have successfully able to find out the prime number but my app has a button("b2") & :onclick("change order") attached to it, so what i need help with is that.

I am trying to set the output of the nested for-loop to an ArrayList but is not able to set is dynamically for each iteration. I basically want to reorder the elements of arraylist with "Collections.reverse()".

x = Integer.parseInt(et1.getText().toString());
y = Integer.parseInt(et2.getText().toString());

for (i = x; i <= y; i++) {

               if (i == 1 || i == 0)
                   continue;
               flag = 1;

               for (j = 2; j <= i / 2; ++j) {
                   if (i % j == 0) {
                       flag = 0;
                       break;
                   }
               }


               if (flag == 1) {
                   myList = new ArrayList<Integer>();
                 
                   for (flag=i; i>0;){
                       myList.add(i);   //this i suppose will do the trick

                   }

                   fResult = new String(myList.toString());
                   t1.setText(fResult);
                   t1.setTextColor(getResources().getColor(R.color.myColor));
                   t2.setTextColor(getResources().getColor(R.color.myColor));
                   Toast.makeText(this, R.string.df, Toast.LENGTH_LONG).show();


               }
           }
       };

public void CloseKeyboard() {
       View view = getCurrentFocus();

       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
       imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
   }

   public void changeOrder(View view) {
       b2 = (Button) findViewById(R.id.myButton2);
       Collections.reverse(myList);
       t1.setText(myList.toString());
       b2.setText(R.string.chnge_text);

   }



Related Questions


What Does MEGABYTE = 1024 * 1024; mean in android

Updated September 01, 2018 03:26 AM