Java替换重复代码的一部分(使用方法?)

时间:2016-11-02 09:37:34

标签: java methods repeat

我有代码,其中部分代码重复,只是名称变量的一些更改,并且可以使用它。但是我有变量声明这个问题,所以我可以在方法之外看到它们,然后在方法内部然后再在方法之外。我是Java的新手,所以需要所有静态,无效和其他所有东西的帮助。 :) 代码自我工作正常,只是认为我应该按照预期开始编写它,而不仅仅是复制它的一部分。

public class Some Class{
  public static void someMethod() throws Exception {
    ArrayList<Product> someArraylist= new ArrayList<>();
    int p;

/* from this part, the code repeats many times, just the name of the Arraylist is changing
so if I could make some method, I would just call, and give it the name of the ArrayList
it would run the code, execute the System.out.println() and code continues */
for (p = 0; p < someArraylist.size(); p++) {
    if (someArraylist.get(p).getName().contains("Name of Product")) 
        System.out.println("it works");
}
... some more code, not repeating...
}
/*when I create method here, it doesn't know the variables created before,
 not to mention that it knows the values of them : 
"int p" and "Arraylist someArraylist" */

public static void replaceCode(Arraylist nameOfArraylist) {
for (p = 0; p < nameOfArraylist.size(); p++) {
    if (nameOfArraylist.get(p).getName().contains("Name of Product")) 
        System.out.println("it works");
}
}

}

好吧,当我看到它写的时候,我看起来很糟糕(我想我错过了一些关于方法的基础知识),但是当我已经写好了,我试着发送它,也许有人帮助我。< / p>

3 个答案:

答案 0 :(得分:0)

您不需要如此早地声明p并在每个循环中重复使用它。您可以在方法中使用标准i变量作为for循环。

事实上,你最好在

中使用for-each循环
for(Product p : nameOfArraylist)
    if(p.getName().contains("Name of Product"))
        System.out.println("It works.");

答案 1 :(得分:0)

据我了解你的问题,处理它有两个可能性。 首先是遍历要检查的每个List。没有必要在for循环之外实例化p:

public static void someMethod() throws Exception {
    List<Product> list1 = ...
    for(int i = 0; i < list1.size(), i++){
        if (list1.get(i).getName().contains("Name of Product")){
            System.out.println("it works");
        }
    }
    List<Product> list2 = ...
    for(int i = 0; i < list2.size(), i++){
        if (list2.get(i).getName().contains("Name of Product")){
            System.out.println("it works");
        }
    }
    //...repeat and repeat and repeat
}

但是,正如您已经提到的那样,将这样的重复代码分离到另一种方法会更好:

public static void someMethod() throws Exception {
    List<Product> list1 = ...
    List<Product> list2 = ...
    List<Product> list3 = ...
    List<Product> list4 = ...
    iterate(list1);
    iterate(list2);
    iterate(list3);
    iterate(list4);      
}

public static void iterate(List<Product> list){
    for(int i = 0; i < list.size(), i++){
        if (list.get(i).getName().contains("Name of Product")){
            System.out.println("it works");
        }
    }
}

答案 2 :(得分:0)

考虑到你有一个Product.java,你可以试试这个:

void checkProductInList(List someArrayList, String productName) {
    // I recommend forEach, it's more user friendly.
    for (Product p : someArraylist) {
        if (p.getName().contains(productName)) { 
            System.out.println("it works");
        }
    }
}

然后你可以在需要的地方调用这样的方法:

checkProductInList(someArrayList, "Keyboard"); // "keyboard" is just a product i typed. It can be anything, as a String.