Answer the question
In order to leave comments, you need to log in
How to access the elements of a two-dimensional collection?
Hello!
Made a two-dimensional collection here. Here she is:
public class Collection {
ArrayList<ArrayList<Integer>> collectionOfPoints;
ArrayList<Integer> subCollectionOfPoints;
int x, y;
public Collection(){
collectionOfPoints = new ArrayList();
x = 0;
y = 0;
}
public void calculatePoints(){
for(int i = 0; i < 10; i++){
x++;
y++;
subCollectionOfPoints = new ArrayList<Integer>();
subCollectionOfPoints.add(x);
subCollectionOfPoints.add(y);
collectionOfPoints.add(subCollectionOfPoints);
}
System.out.println("Коллекция точек создана!");
}
public ArrayList returnCallOfPoints(){
return collectionOfPoints;
}
}
if(ОбъектКлассаСРеализациейДвумернойКоллекции.returnCallOfPoints().size() != 0){
System.out.println(ОбъектКлассаСРеализациейДвумернойКоллекции.returnCallOfPoints().get(1));
}
System.out.println(ОбъектКлассаСРеализациейДвумернойКоллекции.returnCallOfPoints().get(1).get(1));
Answer the question
In order to leave comments, you need to log in
I will not explain that it would be more correct to call it an integer matrix, that it is not necessary to give the class the name of a standard interface, and much more ...
Well, what's the question?
In principle, you argue correctly ...
Just do not return the insides.
better make a setter/getter, where you will check for possible errors, etc.
type
Integer getValueByIndexies(int x, int y){
//всякие проверки, чтоб небыло неконтролируемых эксепшнов, типа аутОфРэндж
return collectionOfPoints.get(x).get(y);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question