在数组中插入一个值,显示包含这个值的数组。

huangapple go评论65阅读模式
英文:

Insert a value in array, show this array with this value

问题

如何打印插入了值的数组?我需要显示初始数组、中间数组和结束数组。

public class ExercicioVetores {

	/*
	 * Program that receives user values,
	 * 1 Value of the array size.
	 * 2 Value of the position where data will be inserted.
	 * 3 Value to be inserted at the specified position.
	 * 4 Print the array with the value inserted at the specified position.
	 * 5 Insert all values and display the complete array.
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Enter the SIZE of the vectorBase you want to create.");

		// Start the method that will retrieve input information from System.in
		Scanner keyboard = new Scanner(System.in);
		// Creating an array with the received parameter.
		int vectorBase[] = new int[keyboard.nextInt()];
		// Initializing the array with zero values.
		Arrays.fill(vectorBase, 0);
		System.out.println("The vectorBase has SIZE = " + vectorBase.length);

		// Displaying the values of the created array
		for (int i : vectorBase) {
			System.out.printf(" " + vectorBase[i]);
		}
		System.out.println("");

		System.out.println("Enter the POSITION where you want to insert a VALUE.");
		int positionValue = keyboard.nextInt();

		System.out.println("Enter the VALUE you want to insert at the POSITION.");
		int insertedValue = keyboard.nextInt();

		System.out.println("The VALUE = " + insertedValue + " will be inserted in vectorBase of SIZE = " + vectorBase.length + ", at POSITION = " + positionValue);
		vectorBase[positionValue] = insertedValue; // Vector Base at positionValue receives the insertedValue.

		// Printing the created array to show the position where the value was inserted correctly.
		// On this point, I'd like to print the array with the value inserted.
		for (int i : vectorBase) {
			System.out.println("");
		}

		/*
		 * Arrays.sort(vectorBase); Sorting the array int showPosition =
		 * Arrays.binarySearch(num,1); Searching for a value
		 */

	}// main

}// class

注意:我已经将代码中的 HTML 编码转换为正常文本,以便更好地阅读。

英文:

How to print the array with this value inserted? I need to show the initial array, the between array and the end array.

public class ExercicioVetores {
/*
* Programa que recebe valores do usuário, 
* 1 Valor do tamanho do vetor. 
* 2 Valor da posiçao onde será inserido um dado 
* 3 Valor á ser inserido na posição informada
* 4 Imprimir o vetor com o valor inserido na posição informada
* 5 Inserir todos os valores e mostrar o vetor completo.
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Digite o TAMANHO do vetorBase que deseja criar.");
//Inicia o método que vai buscar as informações imputadas no System.in
Scanner teclado = new Scanner(System.in); 
// Criando vetor com parametro recebido.
int vetorBase[] = new int[teclado.nextInt()];
// Iniciando vetor com valores zarados
Arrays.fill(vetorBase, 0); 
System.out.println("O vetorBase tem TAMANHO = " + vetorBase.length);
//Mostrando os valores do vetor criado
for (int i : vetorBase) {
System.out.printf(" " + vetorBase[i]);
}
System.out.println("");
System.out.println("Digite a POSIÇÃO que deseja inserir um VALOR.");
int posicaoValor = teclado.nextInt();
System.out.println("Digite o VALOR que deseja inserir na POSIÇÃO.");
int valorInserido = teclado.nextInt();
System.out.println("Será inserido no vetorBase de TAMANHO = " + vetorBase.length + ", na POSIÇÃO = "+ posicaoValor + ", o VALOR = " + valorInserido);
vetorBase[posicaoValor] = valorInserido; //Vetor Base na posicaoValor recebe o valorInserido.
//Imprimir o vetor criado para mostrar a posição que foi inserido o valor na posição correta.
//On this point i like to print the array with the value inserted.
for (int i : vetorBase) {
System.out.println("");
}
/*
* Arrays.sort(vetorBase); Ordenação do vetor int mostrarPosicao =
* Arrays.binarySearch(num,1); Buscar valor
*/
}// main
}// class

答案1

得分: 2

你可以使用以下代码打印数组:

System.out.println(Arrays.toString(vetorBase));

但是以下代码不会打印任何内容:

for (int i : vetorBase) {
    System.out.println("");
}
英文:

you can print the array with

System.out.println(Arrays.toString(vetorBase))

you are not printing anything with:

 for (int i : vetorBase) {
System.out.println("");
}

huangapple
  • 本文由 发表于 2020年8月28日 06:27:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63624956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定