NISHA NAJIHAH
NISHA NAJIHAH

Reputation: 197

Array Copy and add new value

I really don't know how to add in a new value once I copy the array. Does anybody know how I could achieve it?

The output I want:- New Array Value: I Love Coding

public static void main(String[] args) {
        
  String originalArray[] = {"I", "Love","Java"};
  String newArray[] = new String[originalArray.length];
           
  System.arraycopy(originalArray, 0, newArray, 0, 2);  
  System.out.println("Original Array Value: " + Arrays.toString(originalArray));
  System.out.println("New Array Value: " + Arrays.toString(newArray) + Coding);

}

And by the way, can I make the array like this:-

String originalArray[] = {"I Love Java"};
//Instead breaking up to {"I","Love","Java"}
//and if change it to one way is the steps going to be the same and the copy ways too?

Upvotes: 1

Views: 1595

Answers (3)

dreamcrash
dreamcrash

Reputation: 51603

I really don't know how to add in a new value once I copy the array.

You would have to create a new array, copy all the elements of the old array into the new array, and add the new value. Bear in mind, however, that Java ArrayList is more appropriate for such kinds of operations (e.g., add, remove, and so on).

Nonetheless, in your case, to get the output "I Love Coding" you just need to change your code to:

public static void main(String[] args) {
        
  String originalArray[] = {"I", "Love","Java"};
  String newArray[] = new String[originalArray.length];
           
  System.arraycopy(originalArray, 0, newArray, 0, 2);  
  newArray[newArray.length - 1] = "Coding";
  System.out.println("Original Array Value: " + Arrays.toString(originalArray));
  System.out.println("New Array Value: " + Arrays.toString(newArray));

}

And by the way, can I make the array like this:-

String originalArray[] = {"I Love Java"};

Yes you can. However, it would be now an array with a single string "I Love Java", whereas

String originalArray[] = {"I","Love","Java"}

is an array with three strings, namely "I", "Love", "Java". In this case, you would have to replace the String Java with Coding, without having to copy all the array elements.

public static void main(String[] args) {

    String originalArray[] = {"I Love Java"};
    String newArray[] = new String[originalArray.length];
    newArray[0] = originalArray[0].replace("Java", "Coding");
    System.out.println("Original Array Value: " + Arrays.toString(originalArray));
    System.out.println("New Array Value: " + Arrays.toString(newArray));

}

Upvotes: 2

Mustafa Poya
Mustafa Poya

Reputation: 3027

you can replace the last element of your array and then printing them as a single String using String.join method:

String originalArray[] = {"I", "Love","Java"};
originalArray[originalArray.length-1] = "Coding";
System.out.println(String.join(" ", originalArray));

Upvotes: 1

ControlAltDel
ControlAltDel

Reputation: 35106

I think the point of this exercise is that you can change the last element in your new array

newArray[newArray.length - 1] = "Coding";

Then if you print newArray like you were printing the original array, you will see the change

Upvotes: 1

Related Questions