MrCoder
MrCoder

Reputation: 103

String Array or Arraylist, which one to go for?

System.out.print(">");
str = ob.next();
str1 = ob.next();
String substr2[] = str.split(" ");
String substr[] = str1.split(",");
in_stone = Integer.parseInt(substr[0]);
ubound_stone = Integer.parseInt(substr[1]);
player1 = substr[2];
player2 = substr[3];

Output: User enters below..

gamestarts 10,4,U1,U2

player1 stores U1 and player2 stores U2

Now, I have a string array:

  1. List item

String tot_players_game = {"U1,H,B","U2,C,D","U3,E,F"};

I intent to first find U1 and U2 from above string array, then then store the values H,B,C,D as follows.

Name: H B C D

H = firstname, B = surname, U1 = username

What I intending to do is first search U1 stored in Player1 with tot_players_game. Once the string is found, fetch the complete string on that particular index and store in another string variable. Once this is done, will split this value into username, firstname and lastname. I am pretty confused whether to go for arraylist over here or string array? I need to play with this values, as I intent to delete these values as well from array or capture history of each username. Please suggest me the best possible method to implement this..

Upvotes: 0

Views: 127

Answers (3)

Stephen C
Stephen C

Reputation: 718798

Please suggest me the best possible method to implement this..

I think you need to define a simple Player class that has fields for the user identifier, the user's firstname and the user's last name.

Then replace your tot_game_players array with a HashMap<String, Player> map, that you can then populate with the player information so that "U1" maps to Player("U1", "A", "B") and so on.

Instead of searching the tot_game_players array, you can now just call map.get("U1") ... for example.

Representing stuff as arrays or lists of strings is cumbersome, fragile ... and poor OO design.

Upvotes: 0

TTT
TTT

Reputation: 2012

There is no difference between a String[] array and an ArrayList in efficiency. The ArrayList is backed by an Object array no matter the generic type. ArrayList basically offers convenience list-like methods to the backing array, and expanding the array. It is the preferred array to use, though, since it is very likely that the oracle engineers have created a more efficient arraylist than you can with manual array resizing.

Upvotes: 0

MrHaze
MrHaze

Reputation: 3996

In general, this is what you must consider when deciding which one to use

1) First and Major difference between Array and ArrayList in Java is that Array is a fixed length data structure while ArrayList is a variable length Collection class. You can not change length of Array once created in Java but ArrayList re-size itself when gets full depending upon capacity and load factor. Since ArrayList is internally backed by Array in Java, any resize operation in ArrayList will slow down performance as it involves creating new Array and copying content from old array to new array.

2) Another difference between Array and ArrayList in Java is that you can not use Generics along with Array, as Array instance knows about what kind of type it can hold and throws ArrayStoreException, if you try to store type which is not convertible into type of Array. ArrayList allows you to use Generics to ensure type-safety.

3) You can also compare Array vs ArrayList on How to calculate length of Array or size of ArrayList. All kinds of Array provides length variable which denotes length of Array while ArrayList provides size() method to calculate size of ArrayList in Java.

4) One more major difference between ArrayList and Array is that, you can not store primitives in ArrayList, it can only contain Objects. While Array can contain both primitives and Objects in Java. Though Autoboxing of Java 5 may give you an impression of storing primitives in ArrayList, it actually automatically converts primitives to Object.

5) Java provides add() method to insert element into ArrayList and you can simply use assignment operator to store element into Array.

6) One more difference on Array vs ArrayList is that you can create instance of ArrayList without specifying size, Java will create Array List with default size but its mandatory to provide size of Array while creating either directly or indirectly by initializing Array while creating it. By the way you can also initialize ArrayList while creating

Source

Upvotes: 2

Related Questions