Support Online
Skip to main content

List Array and Array List (Array of ArrayList / ArrayList of Array) in Java

🧠 Technical Summary

Main Topic: Creating Array of ArrayList and ArrayList of Array in Java. Problem it solves: It enables the use of fixed size arrays and flexible ArrayList structures together. User Steps:

  1. Creates and populates List Array (List<T>[]).
  2. Creates flexible collection with Array List (ArrayList<T[]>).
  3. Uses ArrayList<Object[]> for different data types.

🔹 1. Array of ArrayList

This structure is a fixed-size array, each element of which is a List object.

⚠️ Generic Array Creation Restriction

Java does not allow Generic arrays like new List<String>[2] to be created due to Type Erasure. This is for runtime security.

💻 Example – Creating a List Array

import java.util.ArrayList;
import java.util.List;

public class JavaListeDizisiOrnek {
public static void main(String[] args) {
List<String> liste1 = new ArrayList<>();
liste1.add("GenixNode");
liste1.add("Bulut");

List<String> liste2 = new ArrayList<>();
liste2.add("tr1");
liste2.add("tr2");
liste2.add("tr3");

// Generic kullanılmaz, derleme hatası almamak için tipi belirtilmez
List<String>[] listeDizisi = new List[2];
listeDizisi[0] = liste1;
listeDizisi[1] = liste2;

for (List<String> liste : listeDizisi) {
System.out.println(liste);
}
}
}

💡 Note: new List<String>[2] cannot be used, it must be defined as new List[2].


🔹 2. ArrayList of Array

This structure is a flexible ArrayList collection, each element of which is an array. This structure fully complies with Generic rules.

💻 Example – ArrayList Consisting of String Arrays

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JavaDiziListesiOrnek {
public static void main(String[] args) {
List<String[]> diziListesi = new ArrayList<>();

String[] dizi1 = { "sunucu", "disk", "ağ" };
String[] dizi2 = { "10.0.1.1", "10.0.1.2", "10.0.1.3" };

diziListesi.add(dizi1);
diziListesi.add(dizi2);

for (String[] dizi : diziListesi) {
System.out.println(Arrays.toString(dizi));
}
}
}

🧠 Advantage: You can store arrays of different lengths in a single list.


🔹 3. Different Types of Arrays (ArrayList of Object[])

If you want to keep arrays of multiple data types in a single list, you can use the ArrayList<Object[]> structure.

💻 Example – Combining String and Integer Arrays

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ObjectDiziListesi {
public static void main(String[] args) {
List<Object[]> liste = new ArrayList<>();

String[] strDizi = { "TR", "EU", "US" };
Integer[] intDizi = { 101, 102, 103 };

liste.add(strDizi);
liste.add(intDizi);

for (Object[] objDizi : liste) {
System.out.println(Arrays.toString(objDizi));

for (Object obj : objDizi) {
if (obj instanceof String) {
// String türü için özel işlem
} else if (obj instanceof Integer) {
// Integer türü için özel işlem
}
}
}
}
}

💬 Note: The instanceof operator enables safe handling of mixed types.


📚 Frequently Asked Questions (FAQ)

1. Why can't generic arrays be created? Generics structure in Java does not preserve type information at run time due to Type Erasure. This makes structures like List<String>[] risky in terms of security.

2. Should Array List or List Array be preferred?

  • Array of ArrayList → When a fixed number of lists is required.
  • ArrayList of Array → Recommended for flexible, dynamic data structures.

3. Where are these structures used? They are preferred in multidimensional data tables, dynamic matrices or JSON-like data organizations.

4. Why is Arrays.toString() necessary? Printing an array directly gives its address in memory. Arrays.toString() displays actual content.


🏁 Conclusion

The combination of array and collection structures in Java makes complex data structures more organized. The ArrayList of Array structure stands out with its flexibility; Array of ArrayList is ideal for fixed structure requirements.

☁️ Try GenixNode Platform now for data management and running your multi-dimensional Java projects with high performance.