如果在Java中找到匹配项,则替换对象数组中的对象元素

时间:2019-03-22 20:51:22

标签: java

在下面的代码中,该方法将使用5个Library对象和1个Library对象的数组作为输入。我想替换Library对象,如果在数组中找到具有相同ID的对象的ID。输出将返回true,并使用替换后的对象值打印5个对象。否则返回false并按原样打印数组。
例如: 如果库对象输入为18,rmgjynm,qxkhlbb 输出将是

Output for performing replace is: false 
33 drfnfuk cqisthj 
78 xhzebcf nnpwbrf 
68 qnoquku qstcebj 
81 ghgwsjm xlewgbj 
47 wgioqsg vjtwscm

即未进行任何更改
如果库对象输入为47,tvistro,rnpholx

Output for performing replace is: true
33 drfnfuk cqisthj 
78 xhzebcf nnpwbrf 
68 qnoquku qstcebj 
81 ghgwsjm xlewgbj 
47 tvistro rnpholx 

我到目前为止编写的代码如下所示。请提出更改建议,因为代码显示无法为inputlib元素找到符号的错误。

public class LibraryDemo 
{
public static Library[] replaceLibraryById(Library[] objArray, Library inputlib)
{
    for(int i=0;i<objArray.length;i++)
    {
        if(inputlib.inputid==objArray[i].id)
        {
            objArray[i].setName=inputlib.inputname;
            objArray[i].setAddress=inputlib.inputadd;
            boolean tf=true;
            System.out.print("Output for performing replace on libraryRes1 is"+tf);
            for(Library:objArray)
            {
                System.out.println(library.getId()+" " + library.getName()+" " + library.getAddress()+" ");
            }
            break;
        }
        else
        {
            boolean tf=false;
            System.out.print("Output for performing replace on libraryRes1 is"+tf);
            for(Library:objArray)
            {
                System.out.println(library.getId()+" " + library.getName()+" " + library.getAddress()+" ");
            }
        }
    }
    return replaceLibraryById();
}
public static void main(String args[]) 
{
    Scanner sc=new Scanner(System.in);
    Library[] objArray=new Library[5];

    for(int i=0;i<objArray.length;i++)
    {
        int id=sc.nextInt();sc.nextLine();
        String name=sc.nextLine();
        String address=sc.nextLine();
        objArray[i]=new Library(id,name,address);
    }

    int inputid=sc.nextInt();sc.nextLine();
    String inputname=sc.nextLine();
    String inputadd=sc.nextLine();
    Library inputlib=new Library(inputid,inputname,inputadd);
    replaceLibraryById(objArray, inputlib);
}
}

class Library
{
int id;
String name;
String address;

public int getId()
{
    return id;
}
public void setId(int id)
{
    this.id=id;
}
public String getName()
{
    return name;
}
public void setName(String name)
{
    this.name=name;
}
public String getAddress()
{
    return address;
}
public void setAddress(String address)
{
    this.address=address;
}


public Library(int id,String name, String address)
{
    this.id=id;
    this.name=name;
    this.address=address;
}
}

2 个答案:

答案 0 :(得分:0)

您的代码充满语法错误。
您的protected static function create_subscription( $user_id, $args ) { $result = false; $purchase = new Recurly_Purchase(); $purchase->currency = $args['currency']; $purchase->collection_method = 'automatic'; $purchase->gateway_code = $args['gateway_code']; $account = new Recurly_Account( $user_id ); $account->email = $args['email']; $account->first_name = $args['billing_first_name']; $account->last_name = $args['billing_last_name']; $account->vat_number = $args['vat_number']; $billing_info = new Recurly_BillingInfo(); $billing_info->token_id = $args['recurly_token']; $account->billing_info = $billing_info; $purchase->account = $account; $subscription = new Recurly_Subscription(); $subscription->plan_code = $args['plan_code']; $purchase->subscriptions = array( $subscription ); try { // "invoice" is the method to transact a Recurly_Purchase. $purchase = Recurly_Purchase::invoice( $purchase ); if( $purchase instanceof Recurly_InvoiceCollection ) { // this seems incredibly janky and error-prone $result = reset( $purchase->charge_invoice->line_items )->subscription->get(); } } catch ( Exception $e ) { $result = $e; } // I need this to return the $subscription object generated by the purchase return $result; } 方法应如下所示:

replaceLibraryById()

另外,最后一行public static Library[] replaceLibraryById(Library[] objArray, Library inputlib) { for(int i=0;i<objArray.length;i++) { if(inputlib.getId()==objArray[i].getId()) { objArray[i].setName(inputlib.getName()); objArray[i].setAddress(inputlib.getAddress()); boolean tf=true; System.out.print("Output for performing replace on libraryRes1 is"+tf); for(Library l :objArray) { System.out.println(l.getId()+" " + l.getName()+" " + l.getAddress()+" "); } break; } else { boolean tf=false; System.out.print("Output for performing replace on libraryRes1 is"+tf); for(Library l :objArray) { System.out.println(l.getId()+" " + l.getName()+" " + l.getAddress()+" "); } } } return replaceLibraryById(); } 是什么意思? 您可以使用适当的参数return replaceLibraryById();调用它,也可以从代码中删除它。

答案 1 :(得分:0)

在这里查看正确的代码

public class findObject {


public static void replaceLibraryById(Library[] objArray, Library inputlib)
{
    boolean tf = false;
    for(int i=0;i<objArray.length;i++)
    {
        if(inputlib.id==objArray[i].id)
        {
            objArray[i].setName(inputlib.name);
            objArray[i].setAddress(inputlib.address);
            tf=true;
            System.out.println("Output for performing replace on libraryRes1 is : "+tf);
            for(Library l:objArray)
            {
                System.out.println(" " + l.getId()+" " + l.getName()+" " + l.getAddress()+" ");
            }
            break;
        }

    }


    if(tf != true) {
        System.out.println("Output for performing replace on libraryRes1 is : "+tf);
        for(Library l:objArray)
        {
            System.out.println(" " + l.getId()+" " + l.getName()+" " + l.getAddress()+" ");
        }
    }

    //return replaceLibraryById();
}


public static void main(String args[]) throws IOException 
{
    Scanner sc=new Scanner(System.in);
    Library[] objArray=new Library[5];

    System.out.println("Create the 5 class data below : ");

    for(int i=0;i<objArray.length;i++)
    {
        System.out.print("enter class id : ");
        int id=sc.nextInt();
        sc.nextLine();

        System.out.print("enter class name : ");
        String name=sc.nextLine();

        System.out.print("enter class Address : ");
        String address=sc.nextLine();

        System.out.println("id :" + id + " name : " + name + " Address :" + address);
        objArray[i]=new Library(id,name,address);
    }

    System.out.println("Search for a class : ");

    System.out.print("enter class id : ");
    int inputid=sc.nextInt();
    sc.nextLine();

    System.out.print("enter class name : ");
    String inputname=sc.nextLine();

    System.out.print("enter class Address : ");
    String inputadd=sc.nextLine();

    Library inputlib=new Library(inputid,inputname,inputadd);
    replaceLibraryById(objArray, inputlib);
}
}

还有图书馆课

class Library
{
int id;
String name;
String address;

public Library(int id,String name, String address)
{
    this.id=id;
    this.name=name;
    this.address=address;
}

public int getId()
{
    return id;
}
public void setId(int id)
{
    this.id=id;
}
public String getName()
{
    return name;
}
public void setName(String name)
{
    this.name=name;
}
public String getAddress()
{
    return address;
}
public void setAddress(String address)
{
    this.address=address;
}

}