1

I am pretty new to java, usually writing python code, so you can downvote me as much as you want if only it will help me with understanding.

Is there more less "javish" way to swap two neighborhood elements of user-input array?

Currently I have:

public static void main(String[] args) {

        Scanner in = new Scanner(System.in);


      ArrayList<Integer> list = new ArrayList<>();
      System.out.println("Enter some numbers:");
      while (in.hasNextInt()) {
          list.add(in.nextInt());
      }


      System.out.println("ArrayList Before: "+ list);
      Collections.swap(list, 0, 1);
      System.out.println("ArrayList After: "+ list);
    }

which changes only first and second ones, but what if I want to change also 3 and 4 as 4 and 3 and so on, is it possible to do it this way?

unigeek
  • 2,656
  • 27
  • 27
Java lava
  • 11
  • 1

2 Answers2

0

For arraylists you can use indexing to get the specific locations. For example, you could use

list.get(i)        //where i is equal to the index you require
list.set(i, x)     // where i is the index, and x is the value that you want to replace it with

EDIT: Good luck with learning java btw, if you are interested in lists and collections take a look into linked lists, they are similar to arraylists but can be more versatile in certain situations.

Connor J
  • 540
  • 1
  • 6
  • 17
  • Note that it needs **a lot** of elements or a very specific scenario for `LinkedList`s to out-perform `ArrayList`s. So, while `LinkedList`, from an algorithmic aspect, is interesting and has advantages, in practice it is out-performed by `ArrayList` in almost every scenario. – Zabuzard Mar 05 '18 at 21:08
  • If performance and size is an issue then yes LinkedList will outperform since nodes can be added with out requiring resizing of the total list. ArrayLists have faster read access, since any index can be accessed, but if the list grows then the system has to create a new list, and then move the old list over, so not as good as LinkedlIst in this case. They both have their advantages in different situations. I meant the linkedlist more as a learning experience for lists in java since its good to know how they work under the hood, and be able to build your own lists when requried. – Connor J Mar 05 '18 at 21:12
  • Yeah, but even if you create a bad scenario for `ArrayList`, it will out-perform `LinkedList` in many cases. It must be a really really bad scenario for `LinkedList` to pay off. It's also cache efficiency and other effects that play a role. The collection team even thought about *deprecating* it (like the `Vector` class) (mentioned [here](https://youtu.be/LgR9ByD1dEw?t=57m7s)). Just wanted to leave it as note in case someone didn't know. – Zabuzard Mar 05 '18 at 21:23
0

You used the Collections.swap method from the API, and according to the API, all you have to do to swap 3 and 4 is change your

Collections.swap(list, 0, 1);

Line to Collections.swap(list, 3, 4);

Look here(the API), for more information.

EDIT: In response to a comment, I would do so in this manner

for(int i = 0; i<list.size()-1; i+=2){
  System.out.println("ArrayList Before: "+ list);
  Collections.swap(list, i,i+1);
  System.out.println("ArrayList After: "+ list);
}

This will swap every index with the one immediately after that. However, if the list is of an odd size, the last index will never be swapped with anything.

  • 1
    OP wants to swap **all elements** with their corresponding neighbors. Not only `0` with `1` and `3` with `4`. He searches a generic solution. – Zabuzard Mar 06 '18 at 02:54