If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
java में ArrayList class एक resizable array है जो कि java.util package की ही एक class है। ArrayList की help से आप array को modify कर सकते हैं, जबकि built in array में आप एक बार array define करने के बाद उसे modify (add new element और delete element) नहीं कर सकते हैं।
OK , तो सवाल अत है कि जब java में already built in array था तो फिर इसकी क्या जरूरत पड़ गयी। built in array और ArrayList में सबसे बड़ा difference यह है कि , built in array को define करने के बाद इसे modify / resize नहीं कर सकते हैं मतलब in future अगर आपको किसी element add या delete करना पड़ा तो आपको new array ही create करना पड़ता। जबकि ArrayList की help से आप array को modify कर सकते हैं।
File : JavaArrayList.java
// import ArrayList class from java.util package.
import java.util.ArrayList;
public class JavaArrayList {
public static void main(String[] args) {
// define String array .
ArrayList bikes = new ArrayList();
bikes.add("Yamaha");
bikes.add("Hero Honda");
bikes.add("Royal Enfield");
System.out.println(bikes);
}
}
[Yamaha, Hero Honda, Royal Enfield]
Example में एक string array list create की गयी है , अब हम इसी array को different - different तरीके से modify करने की कोशिश करेंगे।
ArrayList से किसी item को access करने के लिए get() method का use किया जाता है , जिसमे हमें item index pass करना पड़ता है।
System.out.println(bikes.get(2));
// Output:
Royal Enfield
ArrayList के किसी भी item को update करने के लिए set() method का use किया जाता है , जिसमे हमें item index और updated value pass करना पड़ता है।
System.out.println(bikes.set(1, "Hero"));
System.out.println(bikes);
// Output:
[Yamaha, Hero, Royal Enfield]
ArrayList से किसी item को remove करने के लिए remove() method का use किया जाता है , जिसमे हमें item index pass करना पड़ता है।
bikes.remove(0);
ArrayList से सभी items को remove करने के लिए clear() method का use किया जाता है।
bikes.clear();
ArrayList की length / size पता करने के लिए size() method का use किया जाता है।
File : JavaArrayList.java
import java.util.ArrayList;
public class JavaArrayList {
public static void main(String[] args) {
ArrayList bikes = new ArrayList();
bikes.add("Yamaha");
bikes.add("Hero Honda");
bikes.add("Royal Enfield");
System.out.println("bikes ArrayList have "+ bikes.size() +" items");
}
}
bikes ArrayList have 3 items
ArrayList के सभी elements को आप for Loop की help से iterate भी कर सकते हैं।
File : JavaArrayList.java
import java.util.ArrayList;
public class JavaArrayList {
public static void main(String[] args) {
ArrayList bikes = new ArrayList();
bikes.add("Yamaha");
bikes.add("Hero Honda");
bikes.add("Royal Enfield");
// get total size of ArrayList.
int length = bikes.size();
for(int index=0; index
Yamaha Hero Honda Royal Enfield
जरूरी नहीं है कि ArrayList को सिर्फ for Loop से iterate / traverse किया जाए , आप for-each Loop का use करके भी index के वजाय directly item को access कर सकते हैं।
for(String item : bikes) {
System.out.println( item );
}
// Output :
Yamaha
Hero Honda
Royal Enfield
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook