Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int findNumber(ArrayList<Integer> list, int n) {
- boolean found = false;
- int i = 0;
- int position = 0;
- try {
- while (!found && i < list.size() - 1) {
- for (i = 0; i < list.size() - 1; i++) {
- int k = list.get(i); // for each index in the list, compare i to k
- if (k == n) {
- position = i;
- found = true;
- int temp = list.indexOf(n); // temp = index of n
- if (temp > 0) { // if temp = zero, nothing should happen
- list.remove(n); // n is removed from list
- list.add(temp - 1, n); // n is added to list, one position left of where it was before
- int toReturn = temp; // toReturn = n's former index
- return toReturn;
- }
- }
- }
- }
- } catch (IndexOutOfBoundsException e) {
- e.getMessage();
- }
- if (found) {
- return position;
- } else {
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement