0
what is a collection in Java?
collection
3 Antworten
+ 1
its an array, just better. it can store any data type, and has generic implementation.
it can be dinamically modified, and easier to use and acces. A simple example:
java.util.Collections
ArrayList<Integer> numbers = new ArrayList<Integers>();
for (int i = 0; i <=10;i++){
numbers.add(i);
}
now we can just delete everything that can be divided by 2:
for(int j = 0; i < numbers.size; i++){
if(i % 2 == 0){
numbers.remove(i);
}
}
now, to sort them, just use Collections.sort(List, Comparator)
where List is the list to be sorted, and the Comparator to be used as reference.
this is just the basic stuff, i like to use these. If you have to replace multi dimensional arrays, try Maps there. you can find their methods on oracle docs, i just shared the basics.
+ 1
collection is dynamic storage structure which is used store homogeneous and heterogeneous elements
0
Collections are use in java to store, collect and manipulate data easily.
you can perform actions on data like searching, sorting, update, insert and delete using collections.