aspose file tools*
The moose likes Java in General and the fly likes How to add elements in the middle of Arraylist and linkedlist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to add elements in the middle of Arraylist and linkedlist" Watch "How to add elements in the middle of Arraylist and linkedlist" New topic
Author

How to add elements in the middle of Arraylist and linkedlist

Shahir Deo
Ranch Hand

Joined: Mar 19, 2013
Posts: 80

How to add elements in the middle of Arraylist and linkedlist in Collection list.

Knowledge enlivens the soul.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40


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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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

Joined: Mar 19, 2013
Posts: 80



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

Joined: Mar 19, 2013
Posts: 80

Do we have to Manually update the Index?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 19, 2013
Posts: 80

IndexOutOfBoundsException
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14164
    
  19

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
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

Joined: Mar 19, 2013
Posts: 80



Output:

5
40
30
12


It was So simple as that. Thanks to every one
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38910
    
  23
You’re welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to add elements in the middle of Arraylist and linkedlist