0

I have a for loop

for (int i = 0; i < knowledgeD.size(); i++ )
        knowledgeD.get(i).get(0);

        for (int k = 0; k < knowledgeD.size(); k++ )
            knowledgeD.get(k).get(1);

However I am majorly confused at how I can store all the results of the for loop to one variable I guess?

like say for example i Do this

 for (int i = 0; i < knowledgeD.size(); i++ )
        w =knowledgeD.get(i).get(0);

        for (int k = 0; k < knowledgeD.size(); k++ )
            y = knowledgeD.get(k).get(1);

then in my toString() i do

String s = "{true@" + w + " " +"false@"  + w.complement() + "}" + "@" + this.y;

Where complement() is the complement on the w fraction, in this case 9/11.

This gives me

 {true@135/163 false@28/163}@163/1680}

Which is fantastic, but thats only the last value of the array of arrays

[[2/11, 11/48], [8/35, 35/288], [16/43, 43/288], [75/152, 19/210], [4/5, 5/16], [135/163, 163/1680]]

If anyone can shed light that would be much appreciated. Basically the @ true and @ false are probability of a coinflip being heads or tails.

Cheers Sim PS the datatypes for w and y are BigFractions which are basically fractions, this datatype is in another class.

1
  • In your toString() of which class? (Side note: you could then close your other question :) Commented Mar 29, 2013 at 9:58

1 Answer 1

1

Try this

Just append the next value to the s like this

StringBuilder str="";

for (int i = 0; i < knowledgeD.size(); i++ )
{
        w =knowledgeD.get(i).get(0);
            y = knowledgeD.get(i).get(1);

    str.append("{true@" + w + " " +"false@"  + w.complement() + "}" + "@" + this.y);
     str.append(" ");
}

And in your toString() method just call s=str.toString();

Sign up to request clarification or add additional context in comments.

6 Comments

It would be better to use a StringBuilder in this case to avoid creation of a new String for each append operation.
@niculare yeah really..I'll edit according to your suggestion
hmm thanks i think its working let me double check tho i fixed some stuff up
nevermind fixed again, just changed it to one for loop thanks
@Sim Sorry it was my mistake just remove inner for loop.. check my updated answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.