Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

java - Need help on List of Strings

I am relatively new to java and I would need help on Lists. My requirement is that, I will create a list which stores the user info. I am using List of Strings to achieve this. Right now I am getting the output like [a,b,c,d,e,f,g,h]. This contains 2 records from "A to D" one record and from "e to h" another. Instead of this, I need the output as [a,b,c,d] [e,f,g,h]. Also, I need to use the highlighted separate outputs to insert into User object for which I need to iterate through the output list and add each of them to new User object say for ex: user1 should have [a,b,c,d] and user 2 have [e,f,g,h] and so on.. Can anyone please let me know how do I achieve this.

My code snippet:

User testUser = null;
List<String> userList = new ArrayList<String>();
UserImpl u = new UserImpl();
String userCommonName = null, userEmail = null, userCanonicalName = ull, 
        userPrincipalType = null;

while (pit.hasNext()) {

    testUser = (User) (pit.next());
    userCommonName = testUser.getCommonName();
    userEmail = testUser.getEmail();
    userCanonicalName = testUser.getCanonicalName();
    userPrincipalType = testUser.getPrincipalType();

    userList.add(userCanonicalName);
    userList.add(userCommonName);
    userList.add(userPrincipalType);
    userList.add(userEmail);
    System.out.println(userList);// On printing this I am getting the
                                 // above output which I need to split
                                 // into separate records.
}

The separate records which is in userList has to be inserted back to another User object.

Iterator pit1 = userList.iterator();
while (pit1.hasNext()){
    u.setCanonicalName(userCanonicalName);
    u.setCommonName(userCommonName);
    u.setEmail(userEmail);
    u.setPrincipalType(userPrincipalType);  
}

I am bit lost here. Any help much appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just take

List<String> userList = new ArrayList<String>();

inside the 1st loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...