0

I've learnt how to Create an Arraylist of Objects , such arrays are dynamic in nature. e.g. to create an array of objects(instances of class Matrices ) having 3 fields, the code is like given below :

ArrayList<Matrices> list = new ArrayList<Matrices>();
list.add( new Matrices(1,1,10) );
list.add( new Matrices(1,2,20) );

Moreover, the Matrices class goes like this:

public class Matrices{
int x;
int y;
int z;

Matrices(int x,int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}

Now, how can I access each fields of any element from this array name list ? In particular, how to access the 20 field from 2nd element of this array whose value is (1,2,20) ?

Community
  • 1
  • 1
KNU
  • 2,560
  • 5
  • 26
  • 39

6 Answers6

8

You just use the get method:

// 2nd element; Java uses 0-based indexing almost everywhere
Matrices element = list.get(1);

What you do with the Matrices reference afterwards is up to you - you've shown the constructor call, but we don't know whether those values are then exposed as properties or anything else.

In general, when you're using a class you should look at its documentation - in this case the documentation for ArrayList. Look down the list of methods, trying to find something that matches what you're trying to do.

You should also read the tutorial on collections for more information about the collections library in Java.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • exactly this documentation was all I needed .I'll bookmark it for future reference . thanx – KNU Feb 12 '14 at 09:03
  • Can you also help me in iterating through each elements of ArrayList using for-each construct ? – KNU Feb 13 '14 at 12:15
  • 1
    @KunalKrishna: No, there are *lots* of examples out there. I'd expect pretty much any tutorial on the enhanced for loop to use it as an example. Stack Overflow is great for very specific questions, but it's not a good way of learning a language from scratch. You'd be better off with a good book to work through. – Jon Skeet Feb 13 '14 at 12:39
1

Matrices element = list.get(1); will do the job. ArrayList is a zero index collection. So list.get(1) will give the 2nd element.

You should check relevant apis, here ArrayList

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
0

I see this as a search algorithm question.

You iterate through the list, and check if the current iteration's element contains the desired values.

Georgian
  • 8,795
  • 8
  • 46
  • 87
  • I don't think that's what the OP wants - he's specifically trying to access the 2nd element, so just calling `list.get(1)` does that. – Jon Skeet Feb 12 '14 at 09:02
  • @JonSkeet `In particular, how to access the 20 field from 2nd element of this array whose value is (1,2,20)` this feels rather vague to me. – Georgian Feb 12 '14 at 09:03
  • Yes, but that the "how to access the 20 field" isn't addressed by your answer either... – Jon Skeet Feb 12 '14 at 09:03
0
Matrices m = list.get(1)

read java docs please

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
0

You can get Matrices object from list as:

 Matrices m = list.get(0);// fist element in list
 m.anyPublicMethod();
Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
0
Matrice m = list.get(1);
int twenty = m.getThirdElement(); // or whatever method you named to get the 3rd element (ie 20);

// in one go :
twenty = list.get(1).getThirdElement();
Laurent B
  • 2,200
  • 19
  • 29