Dynamic declaration in JAVA

0

I'm doing a work on graphs and I need to read in the file the data entry of a file, for example:

VERTICES 0 1 2 3 4

ARESTAS 0 1 2 3

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

I'm new to programming and would like to know if it's possible to do this and how I do it, or if there is a better method for that purpose.

    
asked by anonymous 30.10.2015 / 23:53

2 answers

1

This would load a text file of type:

  

1
  2
  3   4

Where 1 2 3 4 would be the coordinates handles 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 of the type 1,2,3,4 direct you will have to learn how to String find each comma and get the number to the left of the comma, no more commas get the last number.

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

Finally% w / w where you create an editor saves a binary file that can be reopened and edited and then interpreted by your program. But this is already intermediate level

    
31.10.2015 / 15:40
0

Every List in Java has a method called add () that is used to add new elements. You do not need to pre-declare how many, as it grows dynamically. I do not understand exactly how your data is represented, but once you already have the data, just do myList.add (myDate).

    
30.10.2015 / 23:56