ArrayList issue

F

francan

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");
 
A

Arne Vajhøj

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
 
R

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help in hangman game 1
How to sort a CSV file with merge sort JAVA 7
Tasks 1
JavaFX tags not wrapping around 0
Issue with textbox script? 0
Confused with the ArrayList 11
Newbee List question 5
String to ArrayList 13

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top