簡體   English   中英

如何更改數組上的字符串

[英]How to Change a String on your Array

好吧,我有很多人。 它們包含實例變量,例如: NAME, AGE, LOCATION 這些是私有實例變量。

現在我的數組中有各種各樣的人擁有自己的實例變量。 我會用我自己來讓它更容易。

奧斯卡 22 美國

然而,在成功創建我的數組后,我需要創建一個布爾方法,讓我可以在我的數組中搜索,找到我的名字並將美國更改為日本(或其他)。 所以如果奧斯卡在日本,那很好,但如果奧斯卡在美國,就把它改成日本。

這個項目應該是一個相當簡單的項目,它不是真正的家庭作業,因為它是一個挑戰,但自從教授分配它已經一個星期了,我無法理解它。 我知道如何做其他方法,但布爾值讓我失望。


public class Frogs {

    private String breed;
    private String nativeTo;


    public Frogs(String breed, String nativeTo)
    {
        this.breed=breed;
        this.nativeTo=nativeTo;

    }

    //accessor(GETTER)

    public String getRegion() 
    {
        return nativeTo;
    }

    public String getBreedName()
    {
        return breed;
    }

    public String toString() ////(RETURNS A STRING (not the memory) ////////////
    {
        return ("Frog Breed -  " +breed+ "From: " +nativeTo);
    }}
public class WildAnimalsDemo {

public static void main(String[]args)
        {

            Frogs [] stuff = new Frogs[6];  //array of objects of type frogs

            stuff[0] = new Frogs ("Tree Frog " , "NJ");
            stuff[1] = new Frogs ("Lizard Frog " , "PA");
            stuff[2] = new Frogs ("Kermit Frog " , "Muppets");
            stuff[3] = new Frogs ("Bear Frog " , "Philipines");
            stuff[4] = new Frogs ("Bull Frog " , "New York");
            stuff[5] = new Frogs ("Lizard " , "PA");           // DOES NOT HAVE "FROG" IN IT

            System.out.println(stuff[0]);
            System.out.println(stuff[1]);
            System.out.println(stuff[2]);
            System.out.println(stuff[3]);
            System.out.println(stuff[4]);
            System.out.println(stuff[5]);
            System.out.println();
            //////////////////////////////////////////////////////////////////////////////////////////////////1stPRINT
            int count=findFrogFromPlace(stuff,"New York");

            System.out.println("Found: " + count);
            //////////////////////////////////////////////////////////////////////
            int count1=findFrogFromPlace(stuff,"PA");

            System.out.println("Found: " + count1);
            /////////////////////////////////////////////////////////////////////////////////////////////////2ndPrint


        /*  boolean possible = findAndSetBreed(stuff, "Frog");
            System.out.println(possible + " Species " + stuff[5] );
            boolean possible1 = findAndSetBreed(stuff, "Frog");
            System.out.println(possible1 + " Species " + stuff[4] );*/

        }

    public static int findFrogFromPlace(Frogs[] F , String theRegion)
    {
        int count=0;             //////THIS COUNT IS NOT THE SAME AS COUNT FROM THE MAIN

        for(int x=0; x<F.length; x++)
        {

               //Returns String/
            if (F[x].getRegion().equalsIgnoreCase(theRegion) )
                count++;
        }

        return count;
    }

    /*public static boolean findAndSetBreed(Frogs[] frog, String theBreed)
    {


        for(int x=0; x<frog.length; x++)
        {


        if (frog[x].getBreedName().equalsIgnoreCase(theBreed) )
                System.out.println(x);

        }
        return frogg;
    }   */
}

您發布的示例與您所問的略有不同。 無論如何,您必須為類的每個字段實現 getter 和 setter 方法。 然后你可以寫一個方法

public boolean checkName(String name) {
   return (name.equals(this.name));
}

在 main 中,您可以通過檢查數組的每個對象來設置正確的位置,如果 checkName 返回 true,則調用 setLocation()

Person[] array={.......}
for(Person p: array) {
   if(p.checkName("Oscar"))
       p.setLocation("Japan");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM