2

I have a piece of code where I am trying to manipulate the contents of an array

loopArrayValuesToRemove(mVal.getValues());

Here I am trying to remove the contents of the array of values

private Values[] loopArrayValuesToRemove(Values[] values) {

    List<Values> files = new ArrayList<>(Arrays.asList(values.clone()));
    Iterator<Values> iterator = files.iterator();
    while(iterator.hasNext()) {
        Values mVal = iterator.next();
        if(!mVal.isCheckedLocally()){
            iterator.remove();
        }
        // other operations
    }
    Values[] mvals = files.toArray(new Values[files.size()]);

    return mvals;
}
  1. Since I am creating the new object even if I manipulate Values[], mVal.getValues() contents are not modified.
  2. It's a Silly question, but I am stuck at this. as I cannot change the response to arrayList in server at (mVal.getValues())
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • And what is your question? – Kayaman Sep 02 '17 at 10:57
  • Question is regarding modifying reference of `mVal.getValues()` contents – Devrath Sep 02 '17 at 10:59
  • You're going to have to explain yourself better than that. What do you want to happen and what is happening? – Kayaman Sep 02 '17 at 11:00
  • I ll try : ....`mVal.getValues()` is having a array of values .... based on a condition I am modifying its contents , In the question I have modified the contents by converting to list since array has fixed size but the issue is, Though list is modified original `mVal.getValues()` remains unchanged – Devrath Sep 02 '17 at 11:03
  • Had you tried to full new list with valid elements instead od removing invalid? Should be faster and less time cinsuming – Beri Sep 02 '17 at 11:16

2 Answers2

2

You're going to have to write it as

mVal.setValues(loopArrayValuesToRemove(mVal.getValues()));

Due to Java passing references by value, you can't change the array itself inside the method. Contents can be changed, but if the size changes it can't be done.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

As I understand your question, your goal is to remove instances from the List which isCheckedLocally() returns true,

You can use the Stream API since Java 8.

mVal = mVal.stream()
    .filter(i -> i.isCheckedLocally())
    .collect(Collectors.toList());
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • there is *// other operations* you can never be sure if the filter by you can solve all of it – Naman Sep 02 '17 at 11:37