0

I have a situation like

String[] ids = new String[depts.size()];
for (int i=0; i<ids.length; i++)
{
  ids [i] = ("Dept" + .... )
}

So the loop looks at ids whose length is set to depts.size. But I need to add another string item to ids [i] like a "Region" + reg_num. Like the next item in ids[i] must be "Region" + reg_num and this is repeated for every department.
Since it is controlled by length, how do I do that? How do I adjust the loop to take 1 additional item. Any suggestions will be appreciated.

Thanks.

4 Answers 4

3

Just use a List instead of an array. Unlike arrays, lists are dynamically resizable:

final List<String> newList = new ArrayList<>(oldList);
newList.add(newElement);

EDIT if you are still using Java 6 you'll have to:

final List<String> newList = new ArrayList<String>(oldList);

instead

EDIT 2 depending on your use case, you may not even need a second list; as lists are dynamically resizable, unless you absolutely need to copy it, just keep the same list:

// first call
list.add(something);
// second call
list.add(somethingElse);
// etc etc

Without seeing more of the code however, it's hard to tell.

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

11 Comments

but in this case a linked list would be better as less overhead when resizing
@luiggi am i missing something?
Thanks for quick response. So are you saying more like final List<String> ids = new ArrayList<>(depts.size()); ids.add("Dept" + dept_num) "Region" + reg_num);
@NimChimpsky That's not necessary unless it's actually causing a problem. I'd recommend using ArrayList over LinkedList unless it's provably causing a slowdown, e.g. it can be seen from a profiler, or the list is actually large enough that ArrayList can't be used (though you'll almost always hit a memory limit before that happens).
@MajidL diamond notation is valid since Java 7. You modified the code for Java 6 and backwards compatibility but never added the note, so rollbacked the answer to its original form.
|
1

If you know before hand then initialize the array accordingly and add the elements as:

   String[] ids = new String[depts.size()+1];//added one in the length
   for (int i=0; i<ids.length; i++)
   {
     ids [i] = ("Dept" + .... )
   }
   ids[ids.length-1] = "Region"+....; 

or

   String[] ids = new String[2*depts.size()];//added one in the length
   for (int i=0; i<ids.length; i=i+2)
   {
     ids [i] = ("Dept" + .... )
     ids[i+1] = "Region"+....; 
   }

5 Comments

the last line of the 1st code snippet will cause an ArrayIndexOutOfBoundsException.
@jlordo: It was a typo. Thanks for raising it. I corrected it.
This is what I was thinking but List seems to be safe as we don't have to worry about the size. Thanks evrybody for your time. Appreciate it.
@user1816974 They both have their pros and cons. Arrays and List or any other type, they all have their own suitability. Hope you understand and decide the right type. For fixed size, probably, array is better choice as it will perform better.
@user1816974 Also I notice, you have accepted the similar answer (second part of my answer).
1

If you are trying to store a department for each id then its better to define a custom class like this:

public class Region {
    String id;
    String dept;
    public Region(String id, String dept) {
        this.id=id;
        this.dept=dept;
    }
    // getters for id and dept
}

then define your region array like this

Region[] regions = new Region[depts.size()];
for (int i=0; i<regions.length; i++) {
  regions[i] = new Region("Dept"+i, "Region"+i);
}

Comments

0

If I understand your reqs correctly, you can do something like this (but remember that you can hold a list of strings instead of an array, to change it dynamically):

String[] ids = new String[depts.size()*2];
for (int i=0; i<ids.length; i+=2)
{
  // depts index would be i/2
  ids [i] = ("Dept" + .... )
  ids[i+1] = "Region"; // + ...
}

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.