Does this program comply with your slogan? Java Arrays [closed]

0

I was doing this arrays program:

  • Write a line of code that declares an array type char and initialize it with the string constant “Estudiando cadenas” . The array must have a size that allows you to save the string without any element.

I did it like this:

public class VectorChar {

public static void main(String[] args) {

 final int TOPE = 16;

 final char arrayCaracter[]= {'E', 's', 't', 'u', 'd', 'i', 
                'a', 'n' ,'d', 'o' , 'c', 'a' , 'd', 'e', 'n' , 'a' , 's'};
  arrayCaracter = new char [TOPE];

    }

}

What I did was declare an array type char with each of its characters in the string "Estudiando cadenas" ; I also put a variable limit that is what will indicate its dimension since in total they are 16 characters  but it gave me error, then look in Google examples of arrays type char and change it to this:

public class VectorChar {

public static void main(String[] args) {


 final char arrayCaracter[]= {'E', 's', 't', 'u', 'd', 'i', 
                'a', 'n' ,'d', 'o' , ' ', 'c', 'a' , 'd', 'e', 'n' , 'a' , 's'};

 String salida = new String(arrayCaracter);

 System.out.println(salida);

    }

The only thing I left was the declaration of the array type char , the String I saw it in an example, so I tried and it worked, but my question is if really in the last change I made in the program is what it asks me in the slogan.

    
asked by computer96 27.11.2018 в 00:34
source

1 answer

1

Actually what you are being asked is that you do something similar to this:

String cadena = "Estudiando cadenas";
char[] array = new char[cadena.length()];
cadena.getChars(0, cadena.length(),array, 0);

Where you first declare the String with the string they give you, then you create an array of char with the size of the string and then fill it using the function getChars

    
answered by 27.11.2018 / 17:19
source