In some situations we use a loop for convenience, but also could perform some operations "manually", example of handling MenuItems from my app:
I create a little array of
MenuItems and then iterate through it using an enhancedforloop.MenuItem[] fileActionsToLock ={ mMenu.findItem(R.id.action_share), mMenu.findItem(R.id.action_rename), mMenu.findItem(R.id.action_copy), mMenu.findItem(R.id.action_move) }; for (MenuItem i : fileActionsToLock) { i.setEnabled(false); i.getIcon().setAlpha(100); }I apply the values separately to each
MenuItem.mMenu.findItem(R.id.action_share).setEnabled(false); mMenu.findItem(R.id.action_share).getIcon().setAlpha(100); mMenu.findItem(R.id.action_rename).setEnabled(false); mMenu.findItem(R.id.action_rename).getIcon().setAlpha(100); mMenu.findItem(R.id.action_copy).setEnabled(false); mMenu.findItem(R.id.action_copy).getIcon().setAlpha(100); mMenu.findItem(R.id.action_move).setEnabled(false); mMenu.findItem(R.id.action_move).getIcon().setAlpha(100);
Since there are only a few elements, the performance is visually the same.
- But which code works faster and why?
- Also, at which point of time the
fileActionsToLockarray and the temporaryMenuItemiwill be garbage collected?