Advertisement
LoganBlackisle

test

Jun 28th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public int findNumber(ArrayList<Integer> list, int n) {
  2. boolean found = false;
  3. int i = 0;
  4. int position = 0;
  5. try {
  6. while (!found && i < list.size() - 1) {
  7. for (i = 0; i < list.size() - 1; i++) {
  8. int k = list.get(i); // for each index in the list, compare i to k
  9. if (k == n) {
  10. position = i;
  11. found = true;
  12. int temp = list.indexOf(n); // temp = index of n
  13. if (temp > 0) { // if temp = zero, nothing should happen
  14. list.remove(n); // n is removed from list
  15. list.add(temp - 1, n); // n is added to list, one position left of where it was before
  16. int toReturn = temp; // toReturn = n's former index
  17. return toReturn;
  18. }
  19. }
  20. }
  21. }
  22. } catch (IndexOutOfBoundsException e) {
  23. e.getMessage();
  24. }
  25. if (found) {
  26. return position;
  27. } else {
  28. return -1;
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement