Ashok
Ashok

Reputation: 311

How to make ArrayList that work as two dimentional array in java?

I want to make arrayList object in java that work as two dimentional array. My question is how can we access value from specific dimention from arrayList.

in two dimentional array, if i want to access value then it can be as m[i][j].

But in arraylist how can i do that ?

Upvotes: 1

Views: 391

Answers (7)

TaherT
TaherT

Reputation: 1335

You cane define like this

1>

List<Object[]> list = new ArrayList<Object[]>();

Fetching

list.get(i)[j];

2>

List<Map<Integer,Object>> list = new ArrayList<Map<Integer,Object>>();

Fetching

list.get(i).get(j);

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234715

Memory is an important consideration here.

It can be acceptable to model a 2D (or higher dimension) array using a 1D container. (This is how the VARIANT SAFEARRAY of Microsoft's COM works.) But, consider this carefully if the number of elements is large; especially if the container allocates a contiguous memory block. Using something like List<List<... will model a jagged-edged matrix and can fragment your memory.

With the 1D approach, you can use the get(index) method on the ArrayList appropriately transformed:

Given the (i)th row and (j)th column, transform using index = i * rows + j where rows is the number of rows in your matrix.

Upvotes: 1

xio4
xio4

Reputation: 216

The solution like List<List<..>> is slow then you should use one dimention array like

    // Two dimentions: m and n

        List<String> arr = new ArrayList<String>(m*n);
        for (int i=0; i< m; ++i) {
           for (int j=0; j<n; ++j) {
             String str=arr.get(i*n + j);
             //You code here
           }
        }

Upvotes: 1

EpicPandaForce
EpicPandaForce

Reputation: 81539

List<List<Integer>> list = new ArrayList<List<Integer>>();
list.add(new ArrayList<Integer>());
list.add(new ArrayList<Integer>());
list.get(0).add(5);
list.get(1).add(6);

for(List<Integer> listiter : list)
{
    for(Integer integer : listiter)
    {
        System.out.println("" + integer);
    }
}

This way you can get the items like

list.get(1).get(0); //second dimension list -> integer

EDIT:

Although it is true that you can use a Map if you are trying to use numeric indices for example for each list, like so:

Map<Integer, List<YourObject>> map = new HashMap<Integer, List<YourObject>>();
map.put(0, new ArrayList<YourObject>());
map.put(5, new ArrayList<YourObject>());
map.get(0).add(new YourObject("Hello"));
map.get(5).add(new YourObject("World"));

for(Integer integer : map.keySet())
{
    for(YourObject yourObject : map.get(integer))
    {
         yourObject.print(); //example method
         System.out.println(" ");
    }
}

Although even then the accessing of Lists would be the same as before,

map.get(0).get(1); //List -> value at index

Obviously you don't need to use Integers as the generic type parameter, that's just a placeholder type.

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347204

You mean something like a List in a List??

May be something like...

List<List<...>> twoDList = new ArrayList<>();

i want to make a List, in which each List key contains another List inside it

It should more like you want some kind of Map, which is basically a key/value pair.

Map<String, List<String>> mapValues = new HashMap<>(25);
List<String> listOfValues = ...;
//...
mapValues.put("A unique key for this list", listOfValues);

//...

List<String> thatListOfValues = mapValues.get("A unique key for this list");

Upvotes: 2

davioooh
davioooh

Reputation: 24676

Also consider Table collection provided by Google Guava library. ArrayTable is an implementation based on 2D array.

Upvotes: 0

Eliott Roynette
Eliott Roynette

Reputation: 734

An arraylist is not an object to make a 2 dimentional arrays. However you can use it anyway : You can use :

new ArrayList<ArrayList<Object>>; //or
new ArrayList<Object[]>; 

But you should implement your own matrix class because you will probably have some check to do and a function get(int row, int column) would be cool

Upvotes: 0

Related Questions