Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding strings to lists - Java

        String t1 = request.getParameter("t1");
        String t2 = request.getParameter("t2");

        List<String> terms = new ArrayList<String>();
        for (int i = 1; i < 51; i++) {
            terms.add(t + i);
        }

Imagine I had vars t1 to t50, is it possible to loop each t using a counter? Something like above, but obvi that doesn't work.

like image 545
mike Avatar asked Jun 08 '11 13:06

mike


1 Answers

You don't need the temporary variables, t1, t2, etc. Otherwise you were on the right track.

    List<String> terms = new ArrayList<String>();
    for (int i = 1; i < 51; i++) {
        terms.add(request.getParameter("t" + i));
    }
like image 130
highlycaffeinated Avatar answered Oct 23 '22 09:10

highlycaffeinated