java - 在数组列表中将我选择的名称设置为非 Activity boolean 值,然后将它们存储在另一个数组中?

标签 java arrays arraylist store

我会尝试简短地解释一下代码。我有一个包含 .txt 文件中的成员的数组列表。在此文件中,我首先使用 boolean 值将所有成员设置为 Activity “true”。我想要使​​用一种方法来执行此操作,该方法接受带有名称的字符串(我想要设置为非 Activity 的 .txt 文件中的人名)将该人设置为非 Activity (假),然后将该人存储在另一个中数组并可能将其从 Activity 列表中删除,以便它不包含非 Activity 成员。

这就是我开始的方式;

public static void inActivateName(ArrayList<Member> register, String name) {
    String s = "";
    int counter = 0;
    for(Member m : register) {
        if(m.getFullName().equals(name)) {
            s += (m) + "\n" ;
            counter++;
        }
    } 
    if(counter < 2) {
        System.out.println("Found the name " + name);
        System.out.println("Do you really want to inactivate" + name + " ? " + " (Y/N) ");
        svar = lasaIn.nextLine();
    } else if(svar.equals("Y")) {
            // Inacivate member ? ? ? 
    } else if(svar.equals("N")) {
        System.out.println("Returning to the main menu");
        Menu.mainMenu();    
    }  else {
    System.out.println("Found the name " + name + " " + counter + " times");
    System.out.println(s);
    System.out.println("Which member do you want to inactivate? (choose by id) ");
    // inActivateId(?) not the problem 
    }
}

在我的成员(member)类(另一个文件)中,我有一个方法将所有当前“成员”设置为 false 使用 ;

public boolean getIsActive() {
    return isActive;
}

构造函数执行以下操作:isActive = true;

我真的不知道如何解决这个问题,非常感谢任何帮助!

最佳答案

我希望以下解决方案适合您。

import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        ArrayList<String> active=new ArrayList<String>();
        active.add("person1");
        active.add("person2");
        active.add("person3");
        ArrayList<String> inactive=new ArrayList<String>();
        deactive("person2",active,inactive);
        System.out.println(active);
        System.out.println(inactive);

     }
     public static void deactive(String person, ArrayList active, ArrayList inactive)
     {
     if(active.contains(person))
     {
        active.remove(person);
        inactive.add(person);
     }
     }
}

如您所见,我有两个数组列表处于 Activity 状态和非 Activity 状态。最初,所有人员都存储在 Activity 数组列表中,非 Activity 数组列表为空。然后我调用一个函数,发送我想要从 Activity 数组中删除的人的姓名。调用该函数后,我打印了两个数组列表。您可以看到特定的人已从 Activity 数组列表中删除并添加到非 Activity 数组列表中。

希望对您有所帮助。

关于java - 在数组列表中将我选择的名称设置为非 Activity boolean 值,然后将它们存储在另一个数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21751064/

相关文章:

java vector 到arraylist

java - 从 ArrayList<URLConnection> 中移除 URLConnection 对象

java - Apache Camel : async operation and backpressure

php - 如果键相同,如何组合两个数组并将其分块

java - spring 构造型注释是否有类似的预构造?

php - 如何在子数组中搜索值并了解该子数组的键索引

javascript - 为什么 Array.prototype.sort 在 Chrome 中有不同的行为?

java - 我在java中有一个字符串表的数组列表,我想从这些表中删除空和空元素

java - 无法通过聚合所有模块来生成覆盖率报告

java - 如何在 Firebase recyclerview 上设置适配器?