コードの一部が繰り返され、名前変数の一部の変更があり、それを使用することができます。しかし、私はこの変数を宣言することでこの問題を抱えています。そのため、メソッドの外側に、次にメソッドの内側に、次にメソッドの外側にそれらを見ることができます。私はJavaにはまったく初心者ですので、静的、空白、その他すべてのものを手助けしてください。 :) コード自己はうまく動作します。ちょうどその部分をコピーするのではなく、意図した通りに書くべきだと思います。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");
}
}
}
まあ、私はそれが書かれて見たとき、それは(私は方法についていくつかのかなりの基礎が欠けていると思う)私とかなり悪いように見えますが、私はそれが既に書かれていたとき、私はそれを送信してみました、多分誰か私に役立ちます。
あなたにはかなり悪いように見える場合は、インデントを修正することをお勧めします。それはあまりにも私に悪い見えます。 – khelwood