Dynamic form statement in JAVA

Asked

Viewed 346 times

0

I am doing a work on graphs and need to read in the file the input of data from a file, example :

VERTICES 0 1 2 3 4

EDGES 0 1 2 3

After reading the file need to instantiate these vertices and edges, would like to know a way by which I can instantiate dynamically, ie create a number of instances according to the number of vertices and edges that were read from the file and then addthem on a list.

I am new to programming and would like to know if it is possible to do this and how I do it, or if there is a better method for this goal.

2 answers

1

This would load a text file of the type:

1
2
3
4

Where 1 2 3 4 would be the coordinates picked up one at a time:

Scanner s = new Scanner(new File("C:/suasCordenadas.txt"));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()){
    list.add(s.next());
}
s.close();

If you want something like 1,2,3,4 direct will have to learn to stir with String locate each comma and take the number to the left of the comma, no longer having commas take the last digit.

Other techniques would be to learn the xml syntax and create your own example tags: <Vertice> 1,2,3,4 </Vertice> or regular expression can also create your tags.

Finally Serializable where you create an editor saves a binary file that can be reopened and edited and then interpreted by your program. But that’s the intermediate level

  • Good afternoon, do not use stacksnippet for codes that cannot be executed, read to understand how it works: http://meta.pt.stackoverflow.com/a/4463/3635 . I’m sure you’ll take my comment as a constructive criticism :)

0

Every Java List has a method called add() that serves to add new elements. You do not need to declare how many they will be, since it grows dynamically. I didn’t understand exactly how your data is represented, but once you already have the data, just scroll.add(myDado).

Browser other questions tagged

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