Skip to main content

Featured post

Simple RecyclerView example with filter option in Android

Hi Guys, Maybe you all are expert in terms of using RecyclerView in android. This blog is simple example for using filter option with RecyclerView adapter. As for now you will instantiate RecyclerView and set the adapter to RecyclerView as following way. RecyclerView list = (RecyclerView) findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(this)); list.setHasFixedSize(true); ArrayList&ltNumber&gt numbers = new ArrayList&lt&gt(); String ONEs[] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN"}; String TENs[] = {"ZERO", "TEN", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY", "HUNDRED"}; String HUNDREDS[] = {"ZERO", "HUNDRED", "TWO HUND

Do you know final size of this ArrayList?

I bet, You will never expect this strange answer. I am leaving it to you right now. Here is the code.
Integer[] list = {2, 4, 6, 8, 10};
ArrayList integerArrayList = new ArrayList<>();
integerArrayList.addAll(Arrays.asList(list));
for (int i = 0; i < integerArrayList.size(); i++) {
    if (integerArrayList.get(i) % 2 == 0) {
        integerArrayList.remove(i);
    }
}

Do you know the size of integerArrayList now? If your answer is 0, then its wrong. Shocked right? Here is the explanation. Let's deep dive into the code now. Add a Log inside the if statement.
Integer[] list = {2, 4, 6, 8, 10};
ArrayList integerArrayList = new ArrayList<>();
integerArrayList.addAll(Arrays.asList(list));
for (int i = 0; i < integerArrayList.size(); i++) {
    if (integerArrayList.get(i) % 2 == 0) {
        Log.v(TAG, "Current list data : " + integerArrayList.toString() + ", integerArrayList.get(" + i + ") : " + integerArrayList.get(i));
        integerArrayList.remove(i);
    }
}

Here is the Logcat output for this Log
Current list data : [2, 4, 6, 8, 10], integerArrayList.get(0) : 2
Current list data : [4, 6, 8, 10], integerArrayList.get(1) : 6
Current list data : [4, 8, 10], integerArrayList.get(2) : 10
So, now you may know.
In our for loop, at our first iteration we have 5 digits in our list, i is 0, size is 5, 0 < 5 condition passed, so we are removing 0th position, which is 2.
At our second iteration we have 4 digits in our list, i is 1, size is 4, 1 < 4 condition passed, and we are removing 1st position, which is 6.
At our third iteration we have 3 digits in our list, i is 2, size is 3, 2 < 3 condition passed, and we are removing 2nd position, which is 10.
At our fourth iteration we have 2 digits remaining in our list, i is 3, size is 2, 3 < 2 condition failed, so finally we have 2 digits our list, they are 4 and 8, still they are dividable by 2.

Interesting right?

If you are really interested in this strange code, then share your solution as comment. How will you remove all of the numbers in the given list which is dividable by 2 or n.



Thank You



Comments

Popular posts from this blog

Simple example of OCRReader in Android.

Hi Friends, Maybe you all heard/used text scanning using camera feature or extracting text from Image. But this sample made it very easy for you. You can made it in very simple line of code. You can download the source code from OCRSample and import the library as a module into your project. Example usage : MainActivity.java public class MainActivity extends AppCompatActivity { private TextView textView; private final int CAMERA_SCAN_TEXT = 0; private final int LOAD_IMAGE_RESULTS = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSele

Simple RecyclerView example with filter option in Android

Hi Guys, Maybe you all are expert in terms of using RecyclerView in android. This blog is simple example for using filter option with RecyclerView adapter. As for now you will instantiate RecyclerView and set the adapter to RecyclerView as following way. RecyclerView list = (RecyclerView) findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(this)); list.setHasFixedSize(true); ArrayList&ltNumber&gt numbers = new ArrayList&lt&gt(); String ONEs[] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN"}; String TENs[] = {"ZERO", "TEN", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY", "HUNDRED"}; String HUNDREDS[] = {"ZERO", "HUNDRED", "TWO HUND

Set limit for fraction in decimal numbers in EditText

            Already we know that we can set which type of input the edittext should accept from user using android:inputType="numberDecimal" But there is no predefined function to set the limit for the edittext to How many digit it should accept after the decimal point from user . We can achieve this by using TextWatcher . Full code example. Following program creates a Decimal Filter. DecimalFilter.java import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.EditText; public class DecimalFilter implements TextWatcher { int count= -1 ; EditText et; Activity activity; public DecimalFilter(EditText edittext, Activity activity) { et = edittext; this.activity = activity; } public void afterTextChanged(Editable s) { if (s.length() > 0) { String str = et.getText().toString(); et.setOnKeyListener(new OnKeyL