Advertisement
LoganBlackisle

test2

Jun 28th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 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. while (!found && i < list.size() - 1) {
  6. for (i = 0; i < list.size() - 1; i++) {
  7. int k = list.get(i); // for each index in the list, compare i to k
  8. if (k == n) {
  9. position = i;
  10. found = true;
  11. int temp = list.indexOf(k); // temp = index of k
  12. if (temp > 0) { // if temp = zero, nothing should happen
  13. list.remove(position); // position is removed from list
  14. list.add(temp - 1, n); // n is added to list, one position left of where it was before
  15. int toReturn = temp; // toReturn = n's former index
  16. return toReturn;
  17. }
  18. }
  19. }
  20. }
  21. if (found) {
  22. return position;
  23. } else {
  24. return -1;
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement