Java에서 언제 varargs를 사용합니까?
나는 varargs를 두려워한다. 무엇을 사용 해야할지 모르겠습니다.
게다가 사람들이 원하는만큼 많은 논쟁을하게하는 것은 위험합니다.
컨텍스트를 사용하기에 적합한 컨텍스트의 예는 무엇입니까?
Varargs 는 불확실한 수의 객체 를 처리해야하는 모든 방법에 유용 합니다 . 좋은 예가 있습니다 String.format
. 형식 문자열은 여러 개의 매개 변수를 사용할 수 있으므로 원하는 수의 개체를 전달할 메커니즘이 필요합니다.
String.format("This is an integer: %d", myInt);
String.format("This is an integer: %d and a string: %s", myInt, myString);
좋은 경험 법칙은 다음과 같습니다.
"입력으로 T (T 형이 무엇이든)의 배열이 필요한 모든 메소드 (또는 생성자)에 varargs를 사용하십시오.
그렇게하면이 메소드를 쉽게 호출 할 수 있습니다 (필요하지 new T[]{...}
않음).
List<T>
이 인수가 입력 전용 인 경우 (즉, 목록이 메소드에 의해 수정되지 않은 경우) 인수 와 함께 메소드를 포함하도록이 규칙을 확장 할 수 있습니다 .
또한 f(Object... args)
API가 명확하지 않은 프로그래밍 방식으로 미끄러지기 때문에 사용을 자제 합니다.
예를 들어, DesignGridLayout 에서 사용 JComponent
하여 한 번의 호출 로 여러을 추가 할 수 있습니다 .
layout.row().grid(new JLabel("Label")).add(field1, field2, field3);
위의 코드에서 add () 메소드는로 정의됩니다 add(JComponent... components)
.
마지막으로, 그러한 메소드의 구현은 빈 vararg로 호출 될 수 있다는 사실을 처리해야합니다! 적어도 하나의 인수를 부과하려면 다음과 같은 추악한 트릭을 사용해야합니다.
void f(T arg1, T... args) {...}
메소드의 구현이 T... args
인수 목록에있는 것보다 덜 간단하기 때문에이 트릭을 추한 것으로 생각 합니다.
이것이 varargs에 대한 요점을 명확히하는 데 도움이되기를 바랍니다.
디버깅 목적으로 varargs를 자주 사용하여 로그에 출력합니다.
내 앱의 거의 모든 클래스에는 debugPrint () 메소드가 있습니다.
private void debugPrint(Object... msg) {
for (Object item : msg) System.out.print(item);
System.out.println();
}
그런 다음 클래스의 메서드 내에서 다음과 같은 호출이 있습니다.
debugPrint("for assignment ", hwId, ", student ", studentId, ", question ",
serialNo, ", the grade is ", grade);
내 코드가 작동한다고 만족하면 로그에 너무 많은 불필요한 정보가 포함되지 않도록 debugPrint () 메서드의 코드를 주석 처리하지만 debugPrint ()에 대한 개별 호출은 주석 처리하지 않은 채로 둘 수 있습니다. 나중에 버그를 발견하면 debugPrint () 코드의 주석 처리를 제거하고 debugPrint ()에 대한 모든 호출이 다시 활성화됩니다.
물론, 나는 varargs를 쉽게 피하고 대신 다음을 수행 할 수 있습니다.
private void debugPrint(String msg) {
System.out.println(msg);
}
debugPrint("for assignment " + hwId + ", student " + studentId + ", question "
+ serialNo + ", the grade is " + grade);
그러나이 경우 debugPrint () 코드를 주석 처리 할 때 결과 문자열로 아무것도 수행되지 않더라도 서버는 여전히 debugPrint ()를 호출 할 때마다 모든 변수를 연결하는 데 어려움을 겪어야합니다. 그러나 varargs를 사용하면 서버는 필요하지 않다는 것을 깨닫기 전에 배열에 배치해야합니다. 많은 시간이 절약됩니다.
Varargs는 메소드에 전달할 인수의 수가 확실하지 않은 경우에 사용할 수 있습니다. 백그라운드에서 지정되지 않은 길이의 매개 변수 배열을 작성하며 이러한 매개 변수는 런타임에서 배열로 처리 될 수 있습니다.
다른 수의 매개 변수를 허용하도록 오버로드 된 메서드가있는 경우 메서드를 다른 시간에 오버로드하는 대신 varargs 개념을 사용할 수 있습니다.
또한 매개 변수 유형이 달라질 때 "Object ... test"를 사용하면 코드가 크게 단순화됩니다.
예를 들면 다음과 같습니다.
public int calculate(int...list) {
int sum = 0;
for (int item : list) {
sum += item;
}
return sum;
}
여기서 간접적으로 int 유형 (목록)의 배열은 매개 변수로 전달되고 코드에서 배열로 처리됩니다.
더 나은 이해를 위해이 링크를 따르십시오 (이 개념을 명확하게 이해하는 데 많은 도움이되었습니다) : http://www.javadb.com/using-varargs-in-java
추신 : 심지어 나는 그것을 믿지 않을 때 varargs를 사용하는 것을 두려워했습니다. 그러나 지금 나는 그것에 익숙합니다. "우리는 알려지지 않은 것을 두려워하며 알려진 것에 집착합니다."최대한 많이 사용하면 원하는대로 시작할 수 있습니다. :)
Varargs는 Java 버전 1.5에 추가 된 기능입니다.
이것을 사용하는 이유는 무엇입니까?
- 메소드에 전달할 인수의 수를 모르는 경우 어떻게합니까?
- 메소드에 무제한의 인수를 전달하려면 어떻게해야합니까?
How this works?
It creates an array with the given arguments & passes the array to the method.
Example :
public class Solution {
public static void main(String[] args) {
add(5,7);
add(5,7,9);
}
public static void add(int... s){
System.out.println(s.length);
int sum=0;
for(int num:s)
sum=sum+num;
System.out.println("sum is "+sum );
}
}
Output :
2
sum is 12
3
sum is 21
I have a varargs-related fear, too:
If the caller passes in an explicit array to the method (as opposed to multiple parameters), you will receive a shared reference to that array.
If you need to store this array internally, you might want to clone it first to avoid the caller being able to change it later.
Object[] args = new Object[] { 1, 2, 3} ;
varArgMethod(args); // not varArgMethod(1,2,3);
args[2] = "something else"; // this could have unexpected side-effects
While this is not really different from passing in any kind of object whose state might change later, since the array is usually (in case of a call with multiple arguments instead of an array) a fresh one created by the compiler internally that you can safely use, this is certainly unexpected behaviour.
I use varargs frequently for constructors that can take some sort of filter object. For example, a large part of our system based on Hadoop is based on a Mapper that handles serialization and deserialization of items to JSON, and applies a number of processors that each take an item of content and either modify and return it, or return null to reject.
In Java doc of Var-Args it is quite clear the usage of var args:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
about usage it says:
"So when should you use varargs? As a client, you should take advantage of them whenever the API offers them. Important uses in core APIs include reflection, message formatting, and the new printf facility. As an API designer, you should use them sparingly, only when the benefit is truly compelling. Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called. "
참고URL : https://stackoverflow.com/questions/766559/when-do-you-use-varargs-in-java
'Programming' 카테고리의 다른 글
이 메소드 서명에서 줄임표 (…)는 무엇입니까? (0) | 2020.05.13 |
---|---|
SQLite 데이터베이스 파일에 가장 적합한 확장자는 무엇입니까? (0) | 2020.05.13 |
React에서 자녀의 상태에 액세스하는 방법? (0) | 2020.05.13 |
IDE를 설치하지 않고 빌드 서버에 VS2017 버전의 msbuild를 어떻게 설치할 수 있습니까? (0) | 2020.05.13 |
N-Tier 아키텍처 란 무엇입니까? (0) | 2020.05.13 |