We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm getting data from an txt file and each line of that txt is transformed into a stringlist. Each time there are stringlists that are equal, a group is created with that stringlists. This "group" is going to be added to an arraylist. This is what I have so far.
What I want to do is get all this "groups"(arraylists) into an array, so I can access all groups, and all individual strings of the stringslists.
Something like this:
This is my code:
int contador = 0;
StringList lista = new StringList();
Via [] listv;
ArrayList <Via> listaVias;
Via ultimaVia=null;
ArrayList <Via>listV=new ArrayList();
ArrayList [] todos = new ArrayList[100];
void setup() {
size(1200, 700);
String lines [] = loadStrings("data.txt");
for (String linha : lines) {
String[] params = split(linha, TAB);
float score = parseFloat(params[0]);
Via via = new Via(score);
for (int i=2; i<params.length; i++) {
via.append(params[i]);
}
if (via.size()>3) {
if (ultimaVia!=null && ultimaVia.isEqual(via)) {
for(int i=0; i<100;i++){
listaVias=new ArrayList();
todos[i].add(listaVias);
listaVias.add(via);
//println(listaVias.get(0));
}
} else {
contador++;
println(contador);
if (contador==100) {
break;
}
}
}
ultimaVia = via;
}
}
This is my class Via:
`class Via extends StringList {
final float score;
Via(float score) {
super();
this.score = score;
}
boolean isEqual(Via via) {
if (this.size() == via.size()) {
for (int i=1; i<via.size();i++) {
if(!via.get(i).equals(this.get(i))){
return false;
}
}
} else {
return false;
}
return true;
}
}
`
SAMPLE OF TXT
Answers
Can you fix your code so that we can read it? Just highlight all your code and press the code button.
Also, what exactly is your question? What does this code do? What did you expect instead?
My code is giving me the error NullPointerException at line 27. I want to call todos[5] (for example) to get the arraylist saved in that index, containing equal stringlists.
Keep in mind that an array of Objects (such as ArrayList) starts out with null values in every index.
If you want an empty ArrayList in each index, then you'll have to initialize every index of that array. Here's an example that uses an array of Strings:
I dont want an empty ArrayList in each index. In each index I want an ArrayList filled with lines of the txt, each line being a Via (that is a StringList).
Each time there are stringlists that are equal, a group is created with that stringlists.
err....
so you want to save 7 times the same stringlist in a group...?
is this part of a bigger project?
Because otherwise, why not save the same stringlist only once (and make a note how often it occured) ?
;-)
before line 27 say
To fill an ArrayList with something, you must first start with an empty ArrayList.
(that sounds like an ancient proverb)
It is part of a bigger project and its not exactly the same stringlist. Just said that to simplify it. They are only different in the first String. What we want to do is save this groups that only differ in the first element.
Sorry for the lack of explanation.
To start with something, you first must empty your mind, young padawan.
unlearn what you've learned.
Chrisir I did this it gives me the error :
Ok I changed todos[i].add(listaVias) to todos[i]=listaVias.
It is now running but it doesnt print anything. I put a println("hi") before the for where todos[i]=listaVias is . It is not printing...
extends
StringList.float
value and stores that in score.float
, as customized Via class, into ArrayList containers.Since the
float
values from the 1st column are stored apart and don't take part inside isEqual(), I think what you're really looking for is a way to map unique StringList (3rd column and on) instances to a set of unique Float values. :-?In short, you want this very complex arrangement:
Map<HashableStringList, Set<Float>>
^#(^@Override
Object's equals() & hashCode() instead.Warning: Since I don't have your "data.txt" TSV file, I can't run & determine if the program is correct! :-\"
@GoToLoop: As we've discussed previously, please do not post full-code solutions. This is not helpful. Instead, try understanding OP's misunderstanding and walk them through the process of solving the problem.
@GoToLoop so every "group" has a unique float? How to get the group with a specific float?
float
value.float
only and vice-versa.float
value as the key in the Map rather than the StringList.float
for each StringList. And vice-versa.float
can't hold such big value. Changed it todouble
.Map<Double, String[]>
container to represent the relationship between the unique Double values w/ its unique String[] array.Map<Double, Set<String>>
.http://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
A more complete & strict alternative version to loadTSVData() w/ more error checks. >:/
B/c it's much bigger, I thought it'd be better to post it apart here and leave the smaller version above for easier comprehension! :-B
sorry, I was not at home often, but now I made this for you
I know it's too late but just to demonstrate what I meant
Best, Chrisir ;-)