• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add elements in the middle of Arraylist and linkedlist

 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to add elements in the middle of Arraylist and linkedlist in Collection list.
 
Henry Wong
author
Marshal
Pie
Posts: 21225
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Have you looked at the JavaDoc for java.util.ArrayList and java.util.LinkedList ?

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saurav Rana, it probably does not help the OP to give a complete answer like that. He will learn much better if he works it out for himself.
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


can anybody explain why cant i execute this and suppose i want to add element at index 1 , how it is done.(because i know it is possible in datastructure but no idea in using code)?
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do we have to Manually update the Index?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shahir Deo wrote:
can anybody explain why cant i execute this


What happens when you try? Is there an error message? Copy/paste the exact, complete error message, and indicate clearly which line is causing it.

and suppose i want to add element at index 1 , how it is done.


Well, the docs for add() say:

Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters:
index - index at which the specified element is to be inserted
element - element to be inserted
Throws:
UnsupportedOperationException - if the add operation is not supported by this list
ClassCastException - if the class of the specified element prevents it from being added to this list
NullPointerException - if the specified element is null and this list does not permit null elements
IllegalArgumentException - if some property of the specified element prevents it from being added to this list
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())


So what part of that don't you understand?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shahir Deo wrote:Do we have to Manually update the Index?


I don't know what you mean by that, but whatever you mean, the answer is probably, "no, that's not even possible," or something close to it.
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IndexOutOfBoundsException
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API documentation for List.add(int index, E element) says:
Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

So, if your list has size 0, then the only index at which you can add anything is index 0.

Trying add(1, "something") or add(3, "something") will result in an IndexOutOfBoundsException.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on what Jesper said:

If you add at index 3, that implies there are 3 elements “before” wherever you are adding. If you have a 2‑element list, you cannot add at position 3 because there cannot be 3 elements “before” wherever you are adding.
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output:

5
40
30
12


It was So simple as that. Thanks to every one
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic