Java Collections: ArrayList
Use a resizable, ordered sequence.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind ArrayList.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
ArrayList stores elements in list order and grows as you add items. Access by index is efficient. Inserting or removing near the front can shift many elements, so consider actual access patterns instead of choosing by name alone.
விரிவான விளக்கங்கள்
ArrayList என்றால் என்ன?
ArrayList என்பது Java-வின் Collections Framework-ல் உள்ள ஒரு முக்கியமான class ஆகும். சாதாரண Arrays-களின் அளவு (size) நிலையானது; அதை மாற்ற முடியாது. ஆனால், ArrayList ஆனது Dynamic Array ஆக செயல்படுகிறது. தரவுகளைச் சேர்க்கச் சேர்க்க, இது தானாகவே தன் அளவை விரிவாக்கிக் கொள்ளும்.
இதனைப் பயன்படுத்த java.util.ArrayList package-ஐ import செய்ய வேண்டும்.
முக்கியமான ArrayList Methods
| Method | செயல்பாடு |
|---|---|
add(element) | பட்டியலின் இறுதியில் புதிய தரவைச் சேர்க்கும். |
get(index) | குறிப்பிட்ட index-ல் உள்ள தரவைப் பெற உதவும். |
set(index, element) | குறிப்பிட்ட index-ல் உள்ள தரவை மாற்றியமைக்கும் (Update/Replace). |
remove(index) | குறிப்பிட்ட index-ல் உள்ள தரவை நீக்கும். |
size() | பட்டியலில் உள்ள மொத்த elements-களின் எண்ணிக்கையை வழங்கும். |
ArrayList-ல் தரவை எடுப்பது (get) மிகவும் வேகமானது — இதன் Time Complexity O(1) ஆகும். ஆனால், பட்டியலின் நடுவில் தரவைச் சேர்ப்பதோ அல்லது நீக்குவதோ (add/remove at index) சற்று தாமதமானது (O(n)), ஏனெனில் மற்ற elements அனைத்தையும் நகர்த்த வேண்டி இருக்கும். அவ்வாறான தேவைகளுக்கு LinkedList சிறந்த தேர்வாகும்.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Generics (<String>) பயன்படுத்துவதன் மூலம் Type Safety உறுதி செய்யப்படுகிறது
List<String> frameworks = new ArrayList<>();
frameworks.add("Angular");
frameworks.add("React");
frameworks.add("Spring Boot");
frameworks.set(1, "Vue.js"); // React-ஐ Vue.js ஆக மாற்றுகிறோம்
frameworks.remove("Angular"); // Angular-ஐ நீக்குகிறோம்
System.out.println("Total frameworks: " + frameworks.size());
// Enhanced For-Loop மூலம் Iteration
for (String fw : frameworks) {
System.out.println(fw);
}
}
}Interview Question: சாதாரண Array மற்றும் ArrayList இடையே உள்ள மிக முக்கிய வேறுபாடு என்ன?
- Array-ல் எண்கள் மட்டுமே சேமிக்க முடியும்; ArrayList-ல் Strings மட்டுமே சேமிக்க முடியும்.
- Array-இன் அளவு (size) நிலையானது (Fixed); ArrayList-இன் அளவு மாறும் தன்மை கொண்டது (Dynamic).
- Array-ஐ loop செய்ய முடியாது; ArrayList-ஐ மட்டுமே loop செய்ய முடியும்.
- எந்த வேறுபாடும் இல்லை, இரண்டும் ஒன்றுதான்.
Reveal answer / விடை
Array-இன் அளவு (size) நிலையானது (Fixed); ArrayList-இன் அளவு மாறும் தன்மை கொண்டது (Dynamic).
சரியான விடை! சாதாரண Array-ஐ உருவாக்கும்போதே அதன் அளவைக் குறிப்பிட்டு விட வேண்டும், பின்னர் அளவை மாற்ற முடியாது. ஆனால் ArrayList தேவைக்கேற்ப தானாகவே சுருங்கி விரியும் (resizable) தன்மையுடையது.
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws Exception {
ArrayList<String> topics = new ArrayList<>();
topics.add("Java");
topics.add("SQL");
topics.set(1, "Collections");
System.out.println(topics);
System.out.println(topics.size());
}
}Predict first. Then reveal the expected output / வெளியீடு
[Java, Collections] 2
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Insert Basics at index 0, then print the list.
Reveal the answer and reasoning / விடை
Use topics.add(0, "Basics");. The order becomes [Basics, Java, Collections].
தவிர்க்க வேண்டிய தவறு: For ArrayList<Integer>, remove(1) removes by index. remove(Integer.valueOf(1)) removes a matching Integer value.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.