0

I have the following code in c++, trying to convert it to java code

The code snippets below shows what I've tried, but its not working

C++ vector<vector<string>> ans; ans.push_back(vector<string>()); for(int k = i, sum = 0; k < n && sum < l[j] ; ++k) { sum += (int) w[k].size(); ans.back().push_back(w[k]); 
Java static Vector<Vector<String>> ans = new Vector<Vector<String>>(); ans.add(new Vector()); for(int k = i, sum = 0; k < n && sum < l[j] ; ++k) { sum += (int) w[k].length(); ans.add(w[k]); 
ans.lastElement().add(w[k]); fixes it 
3
  • 3
    What exactly isn't working? It doesn't compile? It throws an exception? The result isn't expected? Commented Oct 31, 2019 at 5:53
  • managed to fix it. the last line should be ans.get(j).add(w[k]); Commented Oct 31, 2019 at 6:03
  • 2
    @SharhadBashar Then supply an answer to your own question - or remove the question. Commented Oct 31, 2019 at 6:04

1 Answer 1

2

You need to be more specific about what is not working. At the first glance, instead of ans.add(w[k]);, try ans.lastElement().add(w[k]);. Notice how in your c++ code you have ans.back().push_back(w[k]);, not ans.push_back(w[k]);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.