From: francan on 21 Feb 2010 19:45 I have this test working where it takes data from a Java class and shows it in a JSP: String targetItems = "test"; List pageItems = New ArrayList; for(int i = 0;i < calculatedResults + 10;i++) { pageItems.add(targetItems + " " + i) } request.setAttribute(pageItems, "pageItems"); I would like to stop using test and put real data in using an ArrayList but my attempt outputs a huge array of info for each record: ArrayList<TargetBean> targetItems = New ArrayList<TargetBean> (TargetListing.getList()); List pageItems = New ArrayList; for(int i = 0;i < calculatedResults + 10;i++) { pageItems.add(targetItems + " " + i) } request.setAttribute(pageItems, "pageItems");
From: Arne Vajhøj on 21 Feb 2010 21:08 On 21-02-2010 19:45, francan wrote: > I have this test working where it takes data from a Java class and > shows it in a JSP: > String targetItems = "test"; > List pageItems = New ArrayList; > > for(int i = 0;i< calculatedResults + 10;i++) > { > pageItems.add(targetItems + " " + i) > } > request.setAttribute(pageItems, "pageItems"); New ArrayList; does not look as Java that compiles. It is absolutely recommendable to copy paste actual code into questions. Otherwise non relevant error can easily cause the troubleshooting to go in the wrong direction. request.setAttribute(pageItems, "pageItems"); looks as if the arguments should have been swapped. > I would like to stop using test and put real data in using > an ArrayList but my attempt outputs a huge array of info for each > record: > > ArrayList<TargetBean> targetItems = New ArrayList<TargetBean> > (TargetListing.getList()); > > List pageItems = New ArrayList; > for(int i = 0;i< calculatedResults + 10;i++) > { > pageItems.add(targetItems + " " + i) > } > request.setAttribute(pageItems, "pageItems"); I don't understand what this code is supposed to do. You create a new list with each element being the concatenated value of the original list, " " and a number. If I were to make a wild guess then you only need: request.setAttribute("pageItems", TargetListing.getList()); Arne
From: Roedy Green on 22 Feb 2010 01:27 On Sun, 21 Feb 2010 16:45:37 -0800 (PST), francan <francan00(a)yahoo.com> wrote, quoted or indirectly quoted someone who said : >I have this test working where it takes data from a Java class and >shows it in a JSP: >String targetItems = "test"; >List pageItems = New ArrayList; This could not have compiled. new is always lower case. ArrayList needs ( n ); > >for(int i = 0;i < calculatedResults + 10;i++) >{ > pageItems.add(targetItems + " " + i) >} >request.setAttribute(pageItems, "pageItems"); > > >I would like to stop using test and put real data in using >an ArrayList but my attempt outputs a huge array of info for each >record: > >ArrayList<TargetBean> targetItems = New ArrayList<TargetBean> >(TargetListing.getList()); If TargetListing has starts with a capital letter, it SHOULD be the name of class, making getList a static method. >List pageItems = New ArrayList; Again this could not have compiled. >for(int i = 0;i < calculatedResults + 10;i++) >{ > pageItems.add(targetItems + " " + i) to do this, you should declare: ArrayList<String> pageItems = new ArrayList<String>( calculatedResults+10); But since you know the precise size in advance, you could in many circumstances use an array which is many times faster. >} >request.setAttribute(pageItems, "pageItems"); It is best to cut and paste compilable code rather than composing off the top of your head. You introduce additional errors which complicate the issues. See http://mindprod.com/jgloss/arraylist.html for sample code. -- Roedy Green Canadian Mind Products http://mindprod.com When a newbie asks for help tracking a bug in a code snippet, the problem is usually is the code he did not post, hence the value of an SSCCE. see http://mindprod.com/jgloss/sscce.html
|
Pages: 1 Prev: Buttons and Event Handlers Next: ResultSet and String array |