TGStat
TGStat
Type to search
Advanced channel search
  • flag English
    Site language
    flag Russian flag English flag Uzbek
  • Sign In
  • Catalog
    Channels and groups catalog Regional compilations Thematic compilations Платные каналы Search for channels
    Add a channel/group
  • Ratings
    Rating of channels Rating of groups Posts rating
    Ratings of brands and people
  • Analytics
  • Search by posts
  • Telegram monitoring
  • Promotion
    Advertising through Yandex Business Advertising in channels through TGStat Agency Advertising on TGStat.ru website
Java | Фишки и трюки

1 Sep, 12:22

Open in Telegram Share Report

⌨️ Фишка: Создание итератора для пользовательского класса.

Код:
import java.util.Iterator;

public class CustomClass implements Iterable {
private String[] elements;

public CustomClass(String[] elements) {
this.elements = elements;
}

@Override
public Iterator iterator() {
return new CustomIterator();
}

private class CustomIterator implements Iterator {
private int index = 0;

@Override
public boolean hasNext() {
return index < elements.length;
}

@Override
public String next() {
if (this.hasNext()) {
return elements[index++];
} else {
throw new NoSuchElementException();
}
}
}

public static void main(String[] args) {
String[] arr = {"one", "two", "three"};
CustomClass customClass = new CustomClass(arr);

for (String element : customClass) {
System.out.println(element);
}
}
}


💁‍♂️ Этот код позволяет создать итератор для пользовательского класса CustomClass. Итератор позволяет перебирать элементы массива elements и использовать их в цикле for-each.

1.2k 0 14 7
Catalog
Channels and groups catalog Channels compilations Search for channels Add a channel/group
Ratings
Rating of Telegram channels Rating of Telegram groups Posts rating Ratings of brands and people
API
API statistics Search API of posts API Callback
Our channels
@TGStat @TGStat_Chat @telepulse @TGStatAPI
Read
Академия TGStat Telegram Research 2019 Telegram Research 2021 Telegram Research 2023
Contacts
Справочный центр Support Email Jobs
Miscellaneous
Terms and conditions Privacy policy Public offer
Our bots
@TGStat_Bot @SearcheeBot @TGAlertsBot @tg_analytics_bot @TGStatChatBot