[go: up one dir, main page]

0% found this document useful (0 votes)
36 views2 pages

Laborator 9. Programare Orientată Obiect: Utilizarea Containerelor Fara Generics (Containerele de Baza)

The document discusses object-oriented programming in Java using containers (lists) with and without generics. It first shows how a list without generics can add heterogeneous objects like apples and oranges at runtime, but generics allow compile-time checking. Secondly, it demonstrates creating a Gerbil class, adding Gerbil objects to a list, and iterating through the list to call each object's hop method. Thirdly, it explains how to add multiple elements to a container simultaneously using the addAll method. Finally, it provides examples of different ways to iterate through a list, including using an iterator, foreach loop, and removing elements with an iterator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

Laborator 9. Programare Orientată Obiect: Utilizarea Containerelor Fara Generics (Containerele de Baza)

The document discusses object-oriented programming in Java using containers (lists) with and without generics. It first shows how a list without generics can add heterogeneous objects like apples and oranges at runtime, but generics allow compile-time checking. Secondly, it demonstrates creating a Gerbil class, adding Gerbil objects to a list, and iterating through the list to call each object's hop method. Thirdly, it explains how to add multiple elements to a container simultaneously using the addAll method. Finally, it provides examples of different ways to iterate through a list, including using an iterator, foreach loop, and removing elements with an iterator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Laborator 9.

Programare orientată obiect

1. Utilizarea containerelor fara generics (containerele de baza)

import java.util.*;
class Apple {
private static long counter;
private final long id = counter++;
public long id() { return id; }
}
class Orange {}
public class ApplesAndOrangesWithoutGenerics {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ArrayList apples = new ArrayList(); //lista negenerica
for(int i = 0; i < 3; i++)
apples.add(new Apple());
// Not prevented from adding an Orange to apples:
apples.add(new Orange());
//ultimul element - orange - nu va putea fi convertit la
//Apple=>Exceptie
//Orange is detected only at run time
for(int i = 0; i < apples.size(); i++)
((Apple)apples.get(i)).id();
}
}

2. Utilizarea containerelor generice

import java.util.*;
public class ApplesAndOrangesWithGenerics {
public static void main(String[] args) {
//lista cu generics - stocheaza doar ob. Apple
ArrayList<Apple> apples = new ArrayList<Apple>();
for(int i = 0; i < 3; i++)
apples.add(new Apple());
// Compile-time error:
// apples.add(new Orange());
for(int i = 0; i < apples.size(); i++)
System.out.println(apples.get(i).id());
// Using foreach:
for(Apple c : apples)
System.out.println(c.id());
}
}
3. Sa se realizeze o clasa Gerbil cu o data membru de tip int denumita gerbilNumber, initializata in
constructor. Clasa are o metoda hop() care afiseaza valoare datei membru gerbilNumber. Creati un
ArrayList si adaugati obiecte de tip Gerbil in acesta. Utilizati metoda get() pentru a regasi obiectele
, iar pentru fiecare obiect apelati metoda hop(). Faceti astfel incat gerbilNumber sa fie initializat cu
indexul obiectului care numara cate obiecte au fost create din clasa Gerbil.

4. Inserarea in container a mai multor elemente simultan

import java.util.*;
public class AddingGroups {
public static void main(String[] args) {
//colectie de intregi direct initializata
Collection<Integer> collection =new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5));
Integer[] moreInts = { 6, 7, 8, 9, 10 };
//metoda addAll() va adauga la colectie elemente din vectorul moreInts
collection.addAll(Arrays.asList(moreInts));
}
}

5. Iterarea elementelor din containere

import java.util.*;
public class SimpleIteration {
public static void main(String[] args) {
List<Apple> apples = new ArrayList<Apple>();
//iterare cu for si contor i
for(int i = 0; i < 8; i++)
apples.add(new Apple());
//iterare utilizand un iterator atasat colectiei
Iterator<Apple> it = apples.iterator();
while(it.hasNext()) {
Apple p = it.next();
System.out.print(p.id() + ":" + p + " ");
}
System.out.println();
//iterare folosind foreach
for(Apple p : apples)
System.out.print(p.id() + ":" + p + " ");
System.out.println();
//utilizarea iteratorului pt a elimina elemente
it = apples.iterator();
for(int i = 0; i < 6; i++) {
it.next();
it.remove();
}
System.out.println(apples);
}
}

You might also like