Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String array: expand it out of its range?

Status
Not open for further replies.

lukelukeluke

Technical User
Dec 23, 2003
117
CH
Hi there.
Im working on a little chatclient project.
The idea is that im setting up an array for the users. When someone joins, it calls the function adduser(String user) which adds this user to the user array. There will be a removeuser(String user) function too. But the problem is that when I'm setting up the array:

String users[] = {"peter","chulio","rodriguez"};

it has 3 elements: users[0], users[1], users[2]. But what if I now want to add a user to it? Can i add users[3] in any way? There will may be 200 users in the end, how can i put them in?
Should I just build an array with 1000 empty elements? So I can put in 100 users?

Thanks for your ideas.
Greetings, Lucas
 
Use a Vector or ArrayList from the java.util package instead of an array.

--------------------------------------------------
Free Database Connection Pooling Software
 
How about reconfiguring the array each time?
Code:
public String[] pushArray(String[] array, String new)
{
  String[] temp = array;
  array = new String[temp.length + 1];
  System.arraycopy(temp, 0, array, 0, temp.length);
  array[array.length - 1] = new;
  return array;
}

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
chessbot ... why would you do that over using a vector or ArrayList ?

--------------------------------------------------
Free Database Connection Pooling Software
 
sedj, That is how the ArrayList and Vector work, so all Chessbot is doing is reinventing the wheel.

Using an ArrayList or Vector would be more effeicent and less error prone, as it was written by the people who invented the language -- and spent many hours writing algos.
 
True.

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Not true, as far as I know. Both Vector and ArrayList grow in spurts (e.g. initialize at size 10, grow to size 20, 30...) to save on constant redimensioning, which is pretty expensive.
 
There will be a removeuser(String user) function too
If you use String[] as a storage then how will you find the index of the user to be removed? By using a while loop?
I think using a vector will make sense.

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
turkey_clr.gif
 
Just some thoughts.

- ArrayList owns Vector if you don't need synchronized methods. It's faster and offers the same functionality.

- If you have a look at ArrayList or Vector code, it doesn't have anything that any of us could write. By using them, you can avoid the work of doing it yourself, but don`t expect it to be much better.

- You can define the load factor ie increasing size of an ArrayList each time it's full. You can tune it for performance.

- When you delete an element on a Vector /ArrayList, it just makes a loop, finds the element and the copy the rest of them. No magic there.

Cheers.

Dian
 
Not true, as far as I know. Both Vector and ArrayList grow in spurts (e.g. initialize at size 10, grow to size 20, 30...) to save on constant redimensioning, which is pretty expensive.
I was refering to the princible, you are declaring a new one, copying over elements and pointing to the new one in your class. By default the behavior of an Array list is to double which does affect real run time, but asymtotic runtime of insert is still O(n) -- though creating a list from scratch is O(n) and not O(n^2) which it would be if you only increased capacity by one.

library code is writen to be effecient, tried and true. Given the two options -- unless theres a special case -- using the exsisting libraries is going to get you better data structure and for less work on your part.

Just my 2 cents worth
 
The size increases by:
Code:
newCapacity = (oldCapacity * 3) / 2 + 1;
which means:
10, 16, 25, 38, ...
(found in sources of 1.5.0)
which makes much sense for initial capacity of 1 or 100000 (dec).

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top