The Artima Developer Community
Sponsored Link

Java Buzz Forum
Basic Java example program to replace an element at specified index arraylist

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Basic Java example program to replace an element at specified index arraylist Posted: Mar 9, 2016 10:50 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Basic Java example program to replace an element at specified index arraylist
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.Basic java Example program to replace an element at specified index java arrayList.

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  
  4. public class ReplaceArrayList{
  5.  
  6. public static void main(String[] args) {
  7.   
  8. //create an ArrayList object
  9.  ArrayList<Integer> arrayList = new ArrayList<Integer>();
  10.        
  11.         //Add elements to Arraylist
  12.         arrayList.add(1);
  13.         arrayList.add(2);
  14.         arrayList.add(3);
  15.         arrayList.add(4);
  16.         arrayList.add(5);
  17.         arrayList.add(6);
  18.         arrayList.add(7);
  19.         arrayList.add(8);
  20.         arrayList.add(9);
  21.         arrayList.add(10);
  22.         
  23.  
  24.   /*
  25.   To replace an element at the specified index of ArrayList use
  26.   Object set(int index, Object obj) method.
  27.   This Object set(int index, Object obj) method replaces the specified element at the specified
  28.   index in the ArrayList and returns the element previously at the specified position.
  29.   */
  30.  
  31.  arrayList.set(1,23);
  32.      
  33.  System.out.println("ArrayList contains...");
  34.  Iterator itr=arrayList.iterator();
  35.  
  36. while (itr.hasNext()) {
  37.  
  38.  System.out.println(itr.next());
  39.  
  40.  }
  41.   
  42. }
  43. }
     



Output:

  1. ArrayList contains...
  2. 1
  3. 23
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10

Read: Basic Java example program to replace an element at specified index arraylist

Topic: Java Example Program to Remove element from specified index ArrayList Previous Topic   Next Topic Topic: Basic Java program to Remove specified element from TreeSet example

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use