Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- class Grupa {
- String nazwa;
- Student[] studenci;
- public Grupa(String nazwa){
- this.nazwa = nazwa;
- studenci = null;
- }
- public void dodajStudenta(Student student) {
- if (studenci == null)
- studenci = new Student[1];
- else
- studenci = (Student[]) Arrays.copyOf(studenci, studenci.length + 1);
- studenci[studenci.length - 1] = student;
- Arrays.sort(studenci);
- }
- public void usunStudenta(int ind){
- studenci[ind] = studenci[studenci.length-1];
- studenci = (Student[]) Arrays.copyOf(studenci, studenci.length - 1);
- Arrays.sort(studenci);
- }
- public void sklad(){
- System.out.println(this.nazwa);
- for(int i = 0;i < studenci.length;i++){
- System.out.println(studenci[i]);
- }
- }
- }
- @SuppressWarnings("rawtypes")
- class Student implements Comparable {
- String imie;
- String nazwisko;
- Grupa grupa;
- public Student(String imie, String nazwisko, Grupa grupa) {
- this.imie = imie;
- this.nazwisko = nazwisko;
- this.grupa = grupa;
- }
- @Override
- public String toString(){
- return imie + " " + nazwisko;
- }
- @Override
- public int compareTo(Object o) {
- Student p = (Student) o;
- int porownaneNazwiska = nazwisko.compareTo(p.nazwisko);
- if (porownaneNazwiska == 0) {
- return imie.compareTo(p.imie);
- } else {
- return porownaneNazwiska;
- }
- }
- }
- public class Dziekanat {
- public static void main(String[] args){
- Grupa g1 = new Grupa("2ID12");
- g1.dodajStudenta(new Student("Jan","Kowalski",g1));
- g1.dodajStudenta(new Student("Andrzej","Kowalski",g1));
- g1.dodajStudenta(new Student("Jan","Adamczyk",g1));
- g1.dodajStudenta(new Student("Jan","Ziemczyk",g1));
- g1.sklad();
- int x = Arrays.binarySearch(g1.studenci, new Student("Jan","Kowalski",g1));
- System.out.println(x);
- g1.usunStudenta(x);
- g1.sklad();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment