简体   繁体   English

java方法删除数组

[英]java method removal array

I am currently stuck on a java question where i have no idea how to proceed.. really appreciate any help! 我目前陷入一个Java问题,我不知道该如何进行..真的很感谢任何帮助!

The question is: 问题是:

Write a java method, the method receives an integer number indicating the number of accounts, the array that contains the login names and the corresponding passwords and index of the account to be removed. 编写一个Java方法,该方法接收一个指示帐户数量的整数,该数组包含登录名以及要删除的帐户的相应密码和索引。 It deletes the name and password of the account if removeindex is within the acceptable range. 如果removeindex在可接受的范围内,它将删除帐户的名称和密码。 The method returns the updated number of accounts. 该方法返回更新的帐户数。

removeIndex : the index of the account to be removed removeIndex :要删除的帐户的索引

*Assuming arraysize is already created at other methods. *假设已经通过其他方法创建了arraysize。

 public static int removeAccount(int count, String[] nameArr, String[] paswordArr, int removeIndex){

    for(int i=0; i<count;i++){
        String target = nameArr[removeIndex];
        if(target != null){
            // what should i do here?

        }else{
           System.out.println("id does not exist");

       }

    }
    return count;
}

First, you can't "remove" anything from an array; 首先,您无法从数组中“删除”任何内容; once it is created, its size is fixed. 创建后,其大小是固定的。 Therefore it is assumed here that what you want is to set the matching value to null if it isn't already. 因此,这里假设您想要的是将匹配值设置为null(如果尚未设置)。

It is also assumed that the count is within bounds... 还假设该计数在范围之内...

Code: 码:

if (removeIndex >= nameArr.length)
    return count;

if (nameArr[removeIndex] == null)
    return count;

nameArr[removeIndex] = null;
passwordArr[removeIndex] = null;

return count - 1;

Now, the question is imprecise. 现在,问题是不精确的。 Say you have an array with elements: 假设您有一个包含元素的数组:

[ x, y, z ]

and the removal index is 1. What do you want the contents of the array to be after the operation? 并且删除索引为1。操作后您希望数组的内容是什么? Is that [ x, null, z ] or [ x, z, null] ? [ x, null, z ]还是[ x, z, null] The code above assumes the former. 上面的代码假定为前者。

Tested Code 测试代码

Try this code. 试试这个代码。 I've used an ArrayList to store accounts information. 我使用了ArrayList来存储帐户信息。

public class Test {
public static ArrayList accounts=new ArrayList();
public static Scanner scan;
public static void main(String args[]){
    System.out.println("Enter number of accounts to add:");
    scan=new Scanner(System.in);
    int number=scan.nextInt();

    addAccount(number);

    System.out.println("Enter index(account no.) to remove account:\n(e.g. 1,2,3 etc)");
    int removeIndex=scan.nextInt();

    number=removeAccount(number,removeIndex);
    System.out.println("Number of accounts: "+number);
}
public static void addAccount(int n){
    scan=new Scanner(System.in);
    for(int i=0;i<n;i++){
        System.out.println("Enter Username for account "+(i+1)+":");
        accounts.add(scan.nextLine());
        System.out.println("Enter Password for account "+(i+1)+":");
        accounts.add(scan.nextLine());
    }
    System.out.println("Accounts added: "+accounts);
}
public static int removeAccount(int n, int index){
    String target = (String) accounts.get((index*2)-2);
    if(accounts.contains(target)){
        accounts.remove((index*2)-2);
        accounts.remove((index*2)-2);
    }
    System.out.println("After Deletion: "+accounts);
    return n-1;
}

} }


How it works: 这个怎么运作:

  1. It takes no. 不用 of accounts to be added. 要添加的帐户数量。
  2. Adds username and password specified no. 添加指定的用户名和密码。 of accounts into ArrayList . ArrayList的帐户。
  3. Asks for account no. 要求帐号 to be deleted. 被删除。
  4. Deletes the record of given account no. 删除给定帐号的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM