Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created April 28, 2025 20:02
Show Gist options
  • Save thinkphp/3ef414af5e2281ff211e5af0c83087c0 to your computer and use it in GitHub Desktop.
Save thinkphp/3ef414af5e2281ff211e5af0c83087c0 to your computer and use it in GitHub Desktop.
Interfata Iterable
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ColectiePersonalizata<T> implements Iterable<T> {
// Array pentru stocarea elementelor
private Object[] elemente;
private int dimensiune;
private static final int CAPACITATE_IMPLICITA = 10;
public ColectiePersonalizata() {
elemente = new Object[CAPACITATE_IMPLICITA];
dimensiune = 0;
}
/**
* Adaugă un element în colecție
*/
public void adauga(T element) {
if (dimensiune == elemente.length) {
redimensioneaza();
}
elemente[dimensiune++] = element;
}
/**
* Mărește capacitatea array-ului intern
*/
private void redimensioneaza() {
Object[] nouArray = new Object[elemente.length * 2];
System.arraycopy(elemente, 0, nouArray, 0, elemente.length);
elemente = nouArray;
}
/**
* Obține elementul de la index-ul dat
*/
@SuppressWarnings("unchecked")
public T get(int index) {
if (index < 0 || index >= dimensiune) {
throw new IndexOutOfBoundsException("Index: " + index + ", Dimensiune: " + dimensiune);
}
return (T) elemente[index];
}
/**
* Returnează numărul de elemente din colecție
*/
public int dimensiune() {
return dimensiune;
}
/**
* Implementarea metodei necesare din interfața Iterable
* Returnează un Iterator pentru această colecție
*/
@Override
public Iterator<T> iterator() {
return new IteratorColectie();
}
/**
* Clasa internă care implementează interfața Iterator
*/
private class IteratorColectie implements Iterator<T> {
private int pozitieActuala = 0;
@Override
public boolean hasNext() {
return pozitieActuala < dimensiune;
}
@Override
@SuppressWarnings("unchecked")
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return (T) elemente[pozitieActuala++];
}
@Override
public void remove() {
throw new UnsupportedOperationException("Operația de ștergere nu este implementată");
}
}
public static void main(String[] args) {
ColectiePersonalizata<String> colectieStringuri = new ColectiePersonalizata<>();
colectieStringuri.adauga("Primul element");
colectieStringuri.adauga("Al doilea element");
colectieStringuri.adauga("Al treilea element");
System.out.println("Parcurgere colecție de stringuri folosind for-each:");
for (String element : colectieStringuri) {
System.out.println(element);
}
ColectiePersonalizata<Integer> colectieNumere = new ColectiePersonalizata<>();
for (int i = 1; i <= 5; i++) {
colectieNumere.adauga(i * 10);
}
System.out.println("\nParcurgere colecție de numere folosind for-each:");
for (Integer numar : colectieNumere) {
System.out.println(numar);
}
System.out.println("\nAcces direct la elementele colecției de numere:");
for (int i = 0; i < colectieNumere.dimensiune(); i++) {
System.out.println("Element la indexul " + i + ": " + colectieNumere.get(i));
}
System.out.println("\nFolosire explicită a iterator-ului:");
Iterator<Integer> iterator = colectieNumere.iterator();
while (iterator.hasNext()) {
System.out.println("Următorul element: " + iterator.next());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment