Home > database >  How to add members to the back or front of an array list
How to add members to the back or front of an array list

Time:11-16

I am trying to use array lists, and I have a row of coordinates. I would like to shift all the coordinates in this row to the front or to the back. I'm not sure if I am on the correct lines here with the code.

List<Coordinate> coordinates = new ArrayList<>();

void addCoordinateToList(Coordinate singleCoordinate) {
    coordinates.add(singleCoordinate)
}

void addCoordinateToBackList(ArrayList<> coordinateList) {
    for(int i = 0; i < coordinates.size(); i  ) {
        coordinateList.add(i, coordinateList(i));
    }
}

void addCoordinateToFrontList(ArrayList<> coordinateList) {
    for(int i = coordinates.size(); i > 0; i--) {
        coordinateList.add(i, coordinatesList(i));
    }
}

This is not final code, its just writing thoughts out at the moment.

CodePudding user response:

Well, you don't need to use a for loop here.

You could instead utilize the addAll method to achieve what you want:

void addCoordinatesToBack(List<Coordinate> coordinateList) {
    coordinates.addAll(coordinateList);
}

void addCoordinatesToFront(List<Coordinate> coordinateList) {
    coordinates.addAll(0, coordinateList);
}

addAll has two forms:

  • addAll(Collection c) adds all elements of the specified Collection at the end of the list;
  • addAll(int position, Collection c) adds all elements of the specified Collection at the given position. Use position 0 to add them at the beginning of the list.

Also note that I have used List instead of ArrayList with your parameters, as this makes it more flexible.


Note that this is not quite the same as reversing lists.

CodePudding user response:

This functionality is built into the List interface. You can use the following functions for that:

List<String> myList = new ArrayList<>();
//adds to the end of the list
myList.add("reading");
myList.addAll(List.of("the","documentation","first!"));

//adds to the front of the list
myList.add(0,"with");
myList.addAll(0,List.of("It","begins");

CodePudding user response:

Your code

void addCoordinateToBackList(ArrayList<> coordinateList) {
  for(int i = 0; i < coordinates.size(); i  ) {
    coordinateList.add(i, coordinateList.get(i));
  }
}

will endlessly add the first element of the original list to beginning of itself. Note that I added the call to List.get(int).

Your last method will throw an ArrayIndexOutOfBounds exception as index i=coordinates.size() is out of bounds. You probably need to change

void addCoordinateToFrontList(ArrayList<> coordinateList) {
  for(int i = coordinates.size() - 1; i >= 0; i--) {
    coordinateList.add(i, coordinatesList.get(i));
  }
}

But this will again add endlessly the last element of the list to itself.

Probably what you want to achieve is:

List<Object> coordinateList = new ArrayList<>();
...
java.util.Collections.reverse(coordinateList);

This will reverse the order of the elements in the list. Note that it will work on the given list and not return anything. This will work only on modifiable lists.

EDIT: You probably mean to "rotate" the elements, right? There is also the method Collections.rotate(List input, int distance). The documentation:

Rotates the elements in the specified list by the specified distance. After calling this method, the element at index i will be the element previously at index (i - distance) mod list.size(), for all values of i between 0 and list.size()-1, inclusive. (This method has no effect on the size of the list.) For example, suppose list comprises [t, a, n, k, s]. After invoking Collections.rotate(list, 1) (or Collections.rotate(list, -4)), list will comprise [s, t, a, n, k].

This works in both directions.

  •  Tags:  
  • java
  • Related