Softunes
Softunes

Reputation: 13

continuously change array size in java

I had asked a question recently.

String[] name = new String [10000]{};
while (c.moveToNext()){
     .....
.  .. . ..
 .. . 

name[i] = displayName;
i++;
}

Log.d("COUNTS_CONTACTS", Arrays.toString(name));

When I print out COUNT_CONTACTS

I get huge output

[B, Bs, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, .....]

It is actually happening for array size. So, I want to change the array size. how much user have contacts the array size will be same as that.. That's what I want. How to do that?

I was reading the question. This answered worked with ArrayList. And, He did as I did. I don't want list either.

Upvotes: 0

Views: 315

Answers (4)

Firok
Firok

Reputation: 319

Use one variable to store the actual value count of your array. Then use StringBuilder or anything else to join the output string. (actually this is what ArrayList does)

But there is no benefits to manage the data structures rather than using any Collection or Map of java.util

Upvotes: 0

Softunes
Softunes

Reputation: 13

I was thinking that I have a String. Let's take a = "name, other". They were behaving like array. But, I declared them as String. So, I can now separate them by comma/,. Then, I wrote following code..

String name = "";
while (c.moveToNext()){
     .....
.  .. . ..
 .. . 

if (name1.isEmpty()){
            name1 = displayName;
}else {
            name1 = name1 + ", " + displayName;
}
}

String[] words = line.split(",");

Log.d("COUNTS_CONTACTS", Arrays.toString(words));

Then, I got what I wanted..!

Upvotes: 0

Ilmarinen123
Ilmarinen123

Reputation: 586

What you are asking for is not possible in java. Arrays have a fixed size that can not be changed after they were created. You have two options:

  • Use Lists (e.g. ArrayList). The syntax is slightly more verbose but otherwise they offer everything that you need.
  • Copy the Array contents to a new Array when you want to resize. This can be done using calls like this: System.arraycopy() However, copying Arrays around is an CPU intensive operation!

If you only worry about the output, you could also hide parts of your array in your printing code, but then you are basically reinventing the wheel, the *List data structures do something like that (but in a very efficient way)

Upvotes: 1

PaulProgrammer
PaulProgrammer

Reputation: 17620

If you don't want to use List, and don't know the number of contacts you're creating before building the array, you have to build some kind of special logic.

For instance, you can do what you show above, but instead of just dumping the array, you'd need to iterate through, and stop processing when you reach a null value:

for( String contact : name) {
    if ( contact == null) { break; }
    // do whatever with a real contact
}

This works OK if you can guarantee that you'll never have more than 10000 contacts. The second you hit 10001 contacts you'll have to rebuild and redeploy.

Alternately, you can create a new array just a little larger than the last when you run out of space. This is what ArrayList does, but wraps it in a helpful interface.

The takeaway here: Java's Collections are really useful.

Upvotes: 0

Related Questions