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<Number> numbers = new ArrayList<>(); 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...
I bet, You will never expect this strange answer. I am leaving it to you right now. Here is the code.
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.
Here is the Logcat output for this Log
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.
Thank You
Integer[] list = {2, 4, 6, 8, 10}; ArrayListintegerArrayList = 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}; ArrayListintegerArrayList = 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) : 10So, 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
Post a Comment