1

how can i make a user enter a number, which will then shift the array to the right 1. the array cant exceed 50. please help, thanks in advance :)

  List<Integer> list = new ArrayList<Integer>(1);
    public void add(int value) {

      list.add(0, value);
      for(int i = 0; i < array.length; i++) {
         list.add(index, value); // how to make the elements shift to the right?
         if(list.size > 50) {
           list.remove(50);
          }
       }
    }
weston
  • 54,145
  • 21
  • 145
  • 203
BriannaXD
  • 169
  • 2
  • 14

6 Answers6

0

ArrayList shifts elements for you, that's why it has index, look at this answer.

When you create the ArrayList: new ArrayList<Integer>(50) 50 dont define size, define capacity of the ArrayList. When created is empty and size is 0.

List<Integer> list = new ArrayList<Integer>(50);


public void add(int value) {
  if (list.size <= 50) list.remove(list.size() - 1);

  // inserting element at position 0 shifts other elements
  list.add(0, value);

}
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • you don't understand the constructor parameter `initialCapacity`. You were correct before. – weston May 14 '15 at 07:49
  • oh wow really? it was that easy? thought of that but i was like surely its a bit harder – BriannaXD May 14 '15 at 07:52
  • If he use array rather than collections as List, he should handle this manually. – ondermerol May 14 '15 at 07:53
  • ArrayLists grow, that's the problem, the parameter `50` is just the initial size. – weston May 14 '15 at 07:54
  • @weston you mean that initial capacity is getting larger when the list has not enough space? So this answer is not true. – ondermerol May 14 '15 at 07:55
  • @weston In this case 50 is just the reserved memory for that ArrayList but later it can grow up, isn't? – Drumnbass May 14 '15 at 08:06
  • @Drumnbass yes, my comments relate to when he was not removing anything. – weston May 14 '15 at 08:12
  • 2
    You set size to 50, but at some point this arraylist will contain 51 (before the remove). Therefore it will have to resize, so you've tried to be efficient, but it's caused more harm than good. – weston May 14 '15 at 08:16
0
 List<Integer> list = new ArrayList<Integer>(50);

  public void add(int value) {
     if (list.size() == 50)
         list.remove(list.size() -1);

     list.add(value);
  }
ondermerol
  • 524
  • 1
  • 11
  • 29
  • Thanks!! also do you know how i can get the last value of the list. for example if there were 11 elements inside the 50 size list. how would i get the vale of the 11th element? – BriannaXD May 14 '15 at 08:28
  • list.size() -1 gives you the last element's index. – ondermerol May 14 '15 at 08:45
  • You know you are adding new elements at the end of the ArrayList, right? not at the beginning as the OP asked. – manuelvigarcia Dec 21 '16 at 11:48
-1
public class TestList {
   public static void main(String[] args) {

   ArrayList<Integer> arrlist = new ArrayList<Integer>(4);

   // use add() method to add elements in the list
   arrlist.add(15);
   arrlist.add(4);
   arrlist.add(5);

   // adding element 25 at third position
   arrlist.add(2,25);

   for (Integer number : arrlist) {
   System.out.println("List Value = " + number);
   }  
   }
}
somspeaks
  • 534
  • 2
  • 6
  • 11
  • do you know how i can get the last value of the list. for example if there were 11 elements inside the 50 size list. how would i get the vale of the 11th element? – BriannaXD May 14 '15 at 08:28
  • @BriannaXD size() of the list gives you the number of elements; since the indexes start in 0, you can `listName.get(listName.size() - 1)` to get the last value stored in the list `listName`. You could just asked StackOverflow: [https://stackoverflow.com/a/687842/5108777] – manuelvigarcia Dec 21 '16 at 11:52
-1

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). From this "http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html"

so you need just to check if the size of your list is no longer than 50, and add the number in the specified index.

List<Integer> list = new ArrayList<Integer>(50);

  public void add(int value) {

     if (list.size() == 50) // if the size of the array is 50, then remove the last value.
     list.remove(list.size() -1);

     list.add(int index, E element);// you can even choose where position to insert your value.
  }
Yacino
  • 645
  • 2
  • 10
  • 32
-1

In the constructor part what you have defined is the capacity. The default minimum capacity is 10. You know your array cant exceed to 50. There is a chance that there must be less element then 50. So first remain that constructor part empty.

List<Integer> list = new ArrayList<Integer>();
    public void add(int value) {
        if(list.size() <50)
            list.add(0,value);
            else
            {
                list.remove(list.size()-1);
                list.add(0, value);
            }
-2
private List<Integer> list = new ArrayList<Integer>(51);

public void add(int value) {
  list.add(0, value); //other elements are shifted right, you need do nothing else

  //then limit the list to 50 elements
  while(list.size() > 50) list.remove(list.size() - 1);
}

I can't see the rest of the code. I don't know what list length is before add so I'm just guaranteeing it's <= 50 after with a while.

You can specify an initial capacity, if you do, use 51 not 50. It gives the array an initial size that can hold your 50, plus the 51st which is in list for a short period before removal.

weston
  • 54,145
  • 21
  • 145
  • 203
  • why a while? if you think there are more elements use [removeRange](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#removeRange(int,%20int)) no? – Jordi Castilla May 14 '15 at 07:57
  • @JordiCastilla because I can't see the rest of the code. I don't know what list length is before `add` so I'm just guaranteeing it's <= 50 after. – weston May 14 '15 at 08:01
  • why not remove range? also don't you should define capacity to omtimize memory usage? – Jordi Castilla May 14 '15 at 08:03
  • @JordiCastilla because `removeRange` is `protected`. So I would have to subclass `ArrayList`. – weston May 14 '15 at 08:11
  • `list.get(list.size()-1)` because `size()` is the actual number of elements in the list, not the current capacity. – weston May 14 '15 at 08:18