Arraylist bugger

Asked

Viewed 88 times

-1

Using the command .clear of java.util.ArrayList; the command resets the array of my graph object as well but I just wanted it to reset the arraylist sucs, follow the code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class PERT {

    private static Scanner scanner2;

    public static void main(String[] args) throws FileNotFoundException{
        File file = new File ("Arquivos/entrada.txt");
        String aresta = "";
        int tempo = 0;
        int i = 0;
        scanner2 = new Scanner(file);
        Scanner scanner = scanner2.useDelimiter("|");
        ArrayList<Aresta> grafo = new ArrayList<Aresta>();
        ArrayList<String> sucs = new ArrayList<String>();
        while(scanner.hasNext()){
            if(scanner.hasNext()){
                aresta = scanner.next();
                scanner.next();
                tempo = scanner.nextInt();
                scanner.next();
                Aresta adc = new Aresta(aresta, null, null, tempo);
                grafo.add(adc);
                while(scanner.hasNext()){
                    String tmp = scanner.next();
                    if(tmp.equals("/")){
                        if(scanner.hasNextLine())
                        scanner.nextLine();
                        break;
                    }else{
                        sucs.add(tmp);
                    }
                }
            }
            grafo.get(i).setSucessores(sucs);
            sucs.clear();
            i++;
        }
        for (i = 0; i < grafo.size(); i++) {
            System.out.println(grafo.get(i).getAresta());
            System.out.println(grafo.get(i).getTempo());
            System.out.println(grafo.get(i).getSucessores());
        }
        scanner.close();
    }

}
  • The clear what do you mean is the Arraylist right? The Scanner has no "clear method".

  • Sorry for the mistake.

  • @Joãocarlos, I would like to add an addendum on Arraylist: here

1 answer

1


I managed to solve my problem, instead of trying to clear the list, I just instated it again, the code was like this:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class PERT {

    private static Scanner scanner2;

    public static void main(String[] args) throws FileNotFoundException{
        File file = new File ("Arquivos/entrada.txt");
        String aresta = "";
        int tempo = 0;
        int i = 0;
        scanner2 = new Scanner(file);
        Scanner scanner = scanner2.useDelimiter("|");
        ArrayList<Aresta> grafo = new ArrayList<Aresta>();
        ArrayList<String> sucs = new ArrayList<String>();
        while(scanner.hasNext()){
            if(scanner.hasNext()){
                aresta = scanner.next();
                scanner.next();
                tempo = scanner.nextInt();
                scanner.next();
                Aresta adc = new Aresta(aresta, null, null, tempo);
                grafo.add(adc);
                while(scanner.hasNext()){
                    String tmp = scanner.next();
                    if(tmp.equals("/")){
                        if(scanner.hasNextLine())
                        scanner.nextLine();
                        break;
                    }else{
                        sucs.add(tmp);
                    }
                }
            }
            grafo.get(i).setSucessores(sucs);
            System.out.println(grafo.get(i).getAresta());
            System.out.println(grafo.get(i).getTempo());
            System.out.println(grafo.get(i).getSucessores());
            sucs = new ArrayList<String>();
            i++;
        }
        for (i = 0; i < grafo.size(); i++) {
            System.out.println(grafo.get(i).getAresta());
            System.out.println(grafo.get(i).getTempo());
            System.out.println(grafo.get(i).getSucessores());
        }
        scanner.close();
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.