0

I want to add some values in string array dynamically by using for loop. when i debug the code at first for loop it is showing values which are adding.I want to check if any one of the string equals to my given value.but in for loop 2 at index 0 it showing null and at index 1 it showing the string value.

for(int i=0;i<someval;i++) {
  String[] mylist = new String[someval];
  mylist[i]=previousVal;
  System.out.println("Previous Value : " +mylist[i]);
}
for (int j = 0; j <=mylist.length; j++) { 
  if (mylist[j].equals(givenValue) ) {                  {
    System.out.println("your value found in the array");
  }
}
0

1 Answer 1

2

The problem is that you are creating a new array using

mylist = new String...

during each loop iteration.

So, it doesn't really matter if you write something to an array, if the next step consists of throwing that array away.

In other words: make sure that you create the array just once; preferable before entering your loop.

Sign up to request clarification or add additional context in comments.

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.