1

I have a String array with name and id, I need to convert that String array to List of objects.

This is my code:

private List<ObjectAttribute> getDtls(String newVal) {
    ObjectAttribute object = new ObjectAttribute();
    List<ObjectAttribute> objLst = new ArrayList<ObjectAttribute>();
    String[] newImageVal = [step0005.jpg, 172B6846-0073-4E5B-B10A-DDD928994EA6, step0003.jpg, FBC8D143-2CD7-47E6-B323-31A0928A9338]
    for (int i = 0; i <= newImageVal.length - 1; i++) {
        object.setImageName(newImageVal[i]);
        object.setImageId(newImageVal[++i]);
        objLst.add(object);
    }
    return objLst;
}

but there is a problem that it always returns only the last value in objList. Can anyone correct this code.

4 Answers 4

3

move

ObjectAttribute object = new ObjectAttribute();

inside the for loop:

for (int i = 0; i <= newImageVal.length - 1; i++) {
    ObjectAttribute object = new ObjectAttribute();
    object.setImageName(newImageVal[i]);
    object.setImageId(newImageVal[++i]);
    objLst.add(object);
}
Sign up to request clarification or add additional context in comments.

6 Comments

It is a very bad practice to increment the for-loop variable again inside the for-loop body. You with your experience should not teach the beginners such things :)
@HonzaZidek that's an error from OP which was just copy pasted. So you don't have to tell StefanBeike but the OP instead ;)
@Lino: I think that we have a certain responsibility in our answers. We should fix not only their functional errors, but also their design errors. SO answers are very often used by beginners as a source of knowledge.
@HonzaZidek you're free to post your own answer, which points that out. SO encourages different answers, which all answer the same thing in different ways. And if you even improve the code from OP it will possibly get a lot of positive feedback
@Honza Zidek yes of course..., but my answer relates to the original problem. not to some code style guide issues or best practices.
|
1
private List<ObjectAttribute> getDtls(String newVal) {
    List<ObjectAttribute> objLst = new ArrayList<ObjectAttribute>();
    String[] newImageVal = [step0005.jpg, 172B6846-0073-4E5B-B10A-DDD928994EA6, step0003.jpg, FBC8D143-2CD7-47E6-B323-31A0928A9338]
//  String delimiter = ", ";
//  newImageVal = newVal.split(delimiter);
    for (int i = 0; i <= newImageVal.length - 1; i++) {
        ObjectAttribute object = new ObjectAttribute();
        object.setImageName(newImageVal[i]);
        object.setImageId(newImageVal[++i]);
        objLst.add(object);
    }
    return objLst;
}

2 Comments

It is a very bad practice to increment the for-loop variable again inside the for-loop body. This should not be the accepted solution as other SO users use them as model solutions..
Also the condition i <= newImageVal.length - 1 is too optimistic, if the newImageVal does not contain an even number of elements, it will throw ArrayIndexOutOfBoundException.
1

The root cause of your problem is, as @StefanBeike wrote, that you instantiate the object only once before the for-loop and then you just keep rewriting its attributes. Moving the instantiation (= calling of new) inside the for-loop fixes the functionality.

However, apart from this, it is a very bad practice to increment the for-loop variable inside the for-loop body. Thus you obscure your intention and you will get a code which is less readable, harder maintainable and easier to break by later changes.

And the main condition should be i < newImageVal.length-1 to handle the array size safely. (To be 100% sure you do not get ArrayIndexOutOfBoundsException.)

There are multiple better ways.

Increment by 2 in the for-loop "header":

for (int i = 0; i < newImageVal.length-1; i += 2) {
    ObjectAttribute object = new ObjectAttribute();
    object.setImageName(newImageVal[i]);
    object.setImageId(newImageVal[i+1]);
    objLst.add(object);
}

Use while-loop instead of for-loop:

int i = 0;
while (i < newImageVal.length-1) {
    ObjectAttribute object = new ObjectAttribute();
    object.setImageName(newImageVal[i++]);
    object.setImageId(newImageVal[i++]);
    objLst.add(object);
}

1 Comment

Maybe it's a mistake from OP, seems quite special to increase the i at two different places. But anyway. Good answer +1 :)
0

or you may do something like this using the streams way:

AtomicInteger ai = new AtomicInteger(); 
List<ObjectAttribute> objLst = Arrays.stream(newImageVal)
.map(img-> {
   ObjectAttribute object = new ObjectAttribute();
   object.setImageName(img);
   object.setImageId(ai.getAndIncrement()); 
   return obj;
 }).collect(Collectors.toList())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.