Difference between arrays in Java

3

What's wrong with Android not setting the size of the array? I have seen in several places that this should be started with a default value.

What is the internal difference of doing this:

 String[] array1 = new String[99]{};

Or this

 String[] array2 = {};

In addition we also have the split, which takes a string and breaks it by adding directly to the array

String t = "Maçã, Banana, Pera";

What would be the difference when I added it to array1 or array2?

array1 = t.split(",");
array2 = t.split(",");
    
asked by anonymous 24.03.2018 / 17:34

2 answers

3

String[] array1 = new String[99];

By doing this you are initializing a 99-position array in which all these positions have the value null . {} is invalid in this case. In this case, array1.length == 99

String[] array2 = {};

In this case you are initializing an array with no elements. Therefore, array2.length == 0 Doing this is the same thing as doing: String[] array2 = new String[0] .

When you run the split function, it will return you a reference to a new array, in this case a new array of Strings. In this example, the values of array1 and array2 will be the same: ["Maçã", " Banana", " Pera"] , but will be two arrays with different references, that is, array1 != array2 .

So, by assigning the split value in the arrays, you have lost the reference you had to them before, now array1 is no longer an array of 99 positions, but one of 3. As array2 is no longer an empty array, but an array of 3 positions exactly equal to array1 but with another reference.

    
24.03.2018 / 18:08
2

All of them have a fixed value.

When I use the code below, I leave explicit the amount of indexes the array should have, in case 99 strings .

String[] array1 = new String[99];

This is how Java works with array , all and any other way - as far as I know - are mere shortcuts,

When we initialize the variable during creation (see below) , we make a shortcut to the previous code.

String[] array2 = {"Pera", "Uva"};

In this example, the code is automatically converted by the compiler to String[] array2 = new String[2]; . You can confirm this using the code below (I used Java 9.0.1 ) .

import java.util.Arrays;
import java.lang.ArrayIndexOutOfBoundsException;

public class HelloWorld
{
  public static void main(String[] args)
  {
    try {
        String[] array2 = {"Pera", "Uva"};
        array2[2] = "Maçã";

        System.out.println(Arrays.toString(array2));
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Não deu certo. O tamanho do índice foi extrapolado");
      System.out.println( e.toString() );
    }
  }
}

Expected output:

Não deu certo. O tamanho do índice foi extrapolado
java.lang.ArrayIndexOutOfBoundsException: 2

By using split , it changes these array values and automatically assigns a fixed value again.

Example:

String[] array2 = {};
array2 = "Maçã, Banana, Pera".split(",");

Java automatically "converts" this code to a fixed array and then passes its reference to the variable array2 . p>

Ps .: If you'd like to test the codes, you can use link

    
24.03.2018 / 18:17