Difference between Java Arrays

Asked

Viewed 340 times

3

What is the problem in Android of not setting the size of the array? I’ve seen in several places that it should be started with a default value.

What’s 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 sequence of characters and breaks them by adding directly in the array

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

What would be the difference when I add it to array1 or 2?

array1 = t.split(",");
array2 = t.split(",");

2 answers

3


String[] array1 = new String[99];

By doing this you are initiating an array of 99 positions in which all these positions have the value null. The {} are invalid in that case. In that case, array1.length == 99

String[] array2 = {};

In this case you are initiating an array with no element. Logo, 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 a reference to a new array, in this case a new Strings array. In this example, the values of array1 and array2 will be the same: ["Maçã", " Banana", " Pera"], but will be two arrays with different references, ie, array1 != array2.

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

2

They all have a fixed value.

When I use the code below, I make explicit the amount of indexes that the array must have in case 99 strings.

String[] array1 = new String[99];

That’s how the Java works with array, any other way - as far as I know - are mere shortcuts, I explain later.

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 (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 exit:

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

Already the use of split, it alters these values of array and automatically assigns a fixed value again.

Example:

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

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

Ps.: If you want to test the codes, you can use https://www.jdoodle.com/online-java-compiler

Browser other questions tagged

You are not signed in. Login or sign up in order to post.