Respuesta :
Answer:
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args)
- {
- String availablePlayer [] = {"Mike", "Jordan", "Alvin", "Calvin", "Rocky", "John", "Harris", "Faris", "Mark"};
- String userTeam [] = new String[5];
- Scanner input = new Scanner(System.in);
- for(int i=0; i < 5; i++){
- System.out.print("Pick a player: ");
- String target = input.nextLine();
- int found = search(availablePlayer, target);
- if(found != -1){
- userTeam[i] = availablePlayer[found];
- String newArray [] = new String[availablePlayer.length-1];
- for(int j = 0; j < found; j++)
- {
- newArray[j] = availablePlayer[j];
- }
- for(int k = found; k < newArray.length; k++){
- newArray[k] = availablePlayer[k + 1];
- }
- availablePlayer = newArray;
- }
- else{
- System.out.println("Player not found.");
- }
- }
- System.out.println(Arrays.toString(userTeam));
- System.out.println(Arrays.toString(availablePlayer));
- }
- public static int search(String playerList[], String target){
- for(int i=0; i < playerList.length; i++){
- if(playerList[i].equals(target)){
- return i;
- }
- }
- return -1;
- }
- }
Explanation:
The solution code is written in Java.
Firstly, create an array of available players and another array for user team (Line 7 - 8).
Next user Scanner object to get target player from user (Line 12 - 14). To get a target player, a search method has been written (Line 40 - 47) which will return the index where the target is found from the array.
When the index of the target is identified from the available player array, that index will be used to extract the target player from the availablePlayer array and assign it to the current element of the userTeam array (Line 17 - 18).
Since Java doesn't offer in-built method to remove the element from an existing array, the code from 19 -28 are written to handle this removal.
At the end, display the userTeam array and availablePlayer array to console (Line 36 - 37).