Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package grafy;
- import java.util.*;
- class W{
- private List<W> laczenia=new ArrayList<W>();
- private int number;
- W(){
- }
- void addConnection(W w){
- laczenia.add(w);
- }
- void delDuplicates(W w){
- for(int i=0;i<w.laczenia.size();++i){
- for(int j=i+1;j<w.laczenia.size();){
- if(w.laczenia.get(j).equals(w.laczenia.get(i)))w.laczenia.remove(j);
- else ++j;
- }
- }
- }
- void printConnections(W w){
- this.delDuplicates(w);
- System.out.print(this.number+": ");
- while(!laczenia.isEmpty()){
- System.out.print(laczenia.get(0).number+" ");
- laczenia.remove(0);
- }
- System.out.println();
- }
- void setNumber(int i){
- number = i+1;
- }
- }
- public class ListaSasiedztwa {
- public static void main(String[] args){
- Random r = new Random();
- W w;
- List<W> graf = new ArrayList<W>();
- for(int i=0;i<10;++i){
- w = new W();
- w.setNumber(i);
- graf.add(w);
- }
- // /\ stworszyl 10 wierzcholkow
- for(int i=0;i<10;++i){
- for(int j=r.nextInt(10);j>0;--j){
- //if(i==j)continue;
- int x=r.nextInt(10);
- if(i==x)continue;
- graf.get(i).addConnection(graf.get(x));
- }
- }
- for(int i=0;i<10;++i){
- graf.get(i).printConnections(graf.get(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement