import java.util.*;
public class Randoms implements Generator <Pair <String, String>>, Iterable <Integer> {
private char symbol;
private int count;
private int MemberCounting = 0;
private static int [] members;
public Randoms (char symbol){
this.symbol = symbol;
for (int i=0; i!=Countries.DATA.length; i++)
if (Countries.DATA[i][0].charAt(0) == symbol)
MemberCounting ++;
members = new int [MemberCounting];
MemberCounting = 0;
for (int i=0; i!=Countries.DATA.length; i++)
if (Countries.DATA[i][0].charAt(0) == symbol)
members[MemberCounting++] = i;
MemberCounting = -1;
}
@Override
public Iterator<Integer> iterator() {
// TODO Auto-generated method stub
return new Iterator <Integer>(){
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return count <= Countries.DATA.length;
}
@Override
public Integer next() {
// TODO Auto-generated method stub
return count ++;
}
};
}
@Override
public Pair<String, String> next() {
MemberCounting ++;
return new Pair (Countries.DATA[members[MemberCounting]][0], Countries.DATA[members[MemberCounting]][1]);
}
}
import java.util.*;
public interface Message {
public void ShowMessage ();
public static class JustClass {
private static boolean key = false;
public static void ShowAllMessages (List <Message> sm){
if (key == false)
System.out.println("Sorry! But information does not exit!");
else
for (Message s : sm)
s.ShowMessage();
}
}
}
import java.util.*;
import java.lang.reflect.*;
public class MyClass {
private static List <Message> list = new ArrayList <Message> ();
public static void add (String message){
list.add(new Message(){
public void ShowMessage() {
System.out.println(message);
}
});
}
public static void SentToAll (){
Message.JustClass.ShowAllMessages(list);
}
public static void main(String[] args) {
String str = "Hello Java";
for (int i=0; i!=3; i++)
add (str);
SentToAll (); // Sorry! But information does not exit!
Class cl = Message.JustClass.class;
try {
Field field = cl.getDeclaredField("key");
field.setAccessible(true);
field.setBoolean(cl, true);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
SentToAll ();
}
}
Выборка следующей командой