0

Let's say we defined an array with 20 elements. is there any way we can add some objects to the array, without any specific order of course and not just once like String[] t= {"one", "two", ..., "twenty"} ?

String[] t = new String[20];
//I know this won't work
//but something like this:
//t = {"one", "two", "three"}
//and later, add some more
//t = {"four"} ...
10
  • 5
    Can you use ArrayList? Commented Jun 23, 2014 at 5:25
  • yes.. you could do array[5]="five", array[1]="one"; Commented Jun 23, 2014 at 5:26
  • @TheLostMind yes, but it is not easy at least without a for loop ... Commented Jun 23, 2014 at 5:28
  • @user3580294 actually it is a really good idea, but I just wanted to make sure it is not possible to do this with a simple array Commented Jun 23, 2014 at 5:29
  • 1
    @user3580294: Well, almost possible. You still need the layer of abstraction to be able to transparently switch to another backing array (if the array needs to grow). Commented Jun 23, 2014 at 5:32

6 Answers 6

2

There are several ways to initialize the elements in an Array,

String[] t = new String[20];
t[0] = "zero";
t[1] = "one";

You can also use System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) to copy from one to another (if that's what you mean). Here concatenate Array(s) a and b to a new Array c.

String[] a = {"Hello"};
String[] b = {"World"};
String[] c = new String[a.length+b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
System.out.println(Arrays.toString(c));

Output is

[Hello, World]
Sign up to request clarification or add additional context in comments.

Comments

1

You cannot change the size of the array, but you can assign elements at specific positions.

t[3] = "four";

Re-ordering and remembering where the array is supposed to end may or may not become cumbersome.

For more flexible "arrays", people like to use java.util.ArrayList.

Comments

1

You can assign values like this

t[10]  = "ten";
t[11]  = "eleven";

It is better to use ArrayList so there is no need of initializing the size first and it is dynamic too.

Comments

1

Unfortunately, that's not exactly how java arrays work. To instantiate and initialize an array use the syntax...

String[] t = new String[20];
t[0] = "One";
t[1] = "Two";

or,

String[] t = {"One", "Two"};

If you want more control over the array the I'd recommend using an ArrayList object instead where you can add, remove, change, sort the items in the array. For example,

ArrayList t = new ArrayList();
t.add("One");
t.add("Two");
t.remove(0);

Comments

1

ArrayList is a good option for ArrayList supports dynamic arrays that can grow as needed. With arrays you can add elements by specifying the specific position you want to add like adding in position 4 we can do something like array[3] = "four" but for more control arraylist is recommended

Comments

0

Unfortunately, Array should be used in below way only

String[] t = new String[20];
t[0] = "One";
t[1] = "Two";
...
t[19] = "Twenty";

You can try using arrayList: You need not even specify how many elements are expected while initializing.

ArrayList t = new ArrayList();
t.add("One");
t.add("Two");
...
t.add("Twenty");

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.