아하
검색 이미지
생활꿀팁 이미지
생활꿀팁생활
생활꿀팁 이미지
생활꿀팁생활
위용있는나방107
위용있는나방10721.05.07

자바 질문 받아주실수 있나요?

public class CallByRef {

public void increase(int[ ] n) {

for(int i=0; i<n.length; i++) {

n[i]++;

}

}

public static void main(String[ ] args) {

int [ ] ref1={100,800,1000};

CallByRef ref=new CallByRef( );

for(int i=0; i<ref1.length; i++) {

System.out.println("ref1["+i +"] : " +ref1[i] );

}

ref.increase(ref1);

System.out.println( );

for(int i=0; i<ref1.length; i++) {

System.out.println("ref1["+i +"] : " +ref1[i] );

}

}

}

Q : 이런 코드가 있는데 제가 increase 메소드를 지우고 호출을 해보았거든요?

<increase 메소드 포함하여 코드 실행 결과>

ref1[0] : 100

ref1[1] : 800

ref1[2] : 1000

ref1[0] : 101

ref1[1] : 801

ref1[2] : 1001

<increase 메소드 호출 안했을 때 코드 실행 결과>

ref1[0] : 100

ref1[1] : 800

ref1[2] : 1000

ref1[0] : 100

ref1[1] : 800

ref1[2] : 1000

이렇게 나오는데 increase 메소드가 어떤 작업을 해주길래 위와 같은 결과가 나오는걸까요...??

public static void main(String[ ] args) {

int num1=100;

int num2=150;

int sum=0;

객체를 생성하고 sum을 계산하는 코드는 어떤식으로 짜는게 좋을까요..?ㅠ

너무 초보적인 건가요..흠..

55글자 더 채워주세요.
답변의 개수5개의 답변이 있어요!
  • 탈퇴한 사용자
    탈퇴한 사용자21.05.09

    CallByRef 클래스의 increase 가 해주는 것은 인자로 받은 배열의 값을 ++ 해주는 것 밖에는 없습니다.

    따라서 100 은 101로 800은 801로 1000은 1001로 값이 증가하게 되는 것입니다.

    프로그램 관점에서는 CallByRef 라는 클래스는 상태를 가지고 있지 않습니다.
    따라서 아래와 같이 바꾸어도 동일하게 동작하게 됩니다.

    public class CallByRef { public static void increase(int[] n) { for (int i = 0; i < n.length; i++) { n[i]++; } } public static void main(String[] args) { int[] ref1 = {100, 800, 1000}; for (int i = 0; i < ref1.length; i++) { System.out.println("ref1[" + i + "] : " + ref1[i]); } CallByRef.increase(ref1); System.out.println(); for (int i = 0; i < ref1.length; i++) { System.out.println("ref1[" + i + "] : " + ref1[i]); } } }

    increase 를 main 함수 안으로 옮기면 아래와 같은 코드인 셈입니다.

    public class CallByRef { public static void main(String[] args) { int[] ref1 = {100, 800, 1000}; for (int i = 0; i < ref1.length; i++) { System.out.println("ref1[" + i + "] : " + ref1[i]); } for (int i = 0; i < ref1.length; i++) { ref1[i]++; } System.out.println(); for (int i = 0; i < ref1.length; i++) { System.out.println("ref1[" + i + "] : " + ref1[i]); } } }

    절차지향적인 위의 코드를 좀더 객체지향적으로 바꾼다면 아래와 같이 바꿀 수 있겠습니다.

    public class CallByRef { private final int[] ref1; public CallByRef(int[] ref1) { this.ref1 = ref1; } public void increase() { for (int i = 0; i < ref1.length; i++) { ref1[i]++; } } public void printValues() { for (int i = 0; i < ref1.length; i++) { System.out.println("ref1[" + i + "] : " + ref1[i]); } } public static void main(String[] args) { int[] data = {100, 800, 1000}; CallByRef ref = new CallByRef(data); ref.printValues(); ref.increase(); System.out.println(); ref.printValues(); } }

    객체를 생성하고 sum을 계산하는 코드는 여러가지 방법이 있겠지만 아래와 같이 누진기(Accumulator)를 만들 수 있겠네요.

    public class Accumulator { private int sum = 0; public int getSum() { return sum; } public void add(int value) { sum += value; } public static void main(String[] args) { Accumulator accumulator = new Accumulator(); accumulator.add(100); accumulator.add(150); int sum = accumulator.getSum(); System.out.println(sum); } }


  • 안녕하세요.

    int [ ] ref1={100,800,1000}; // 변수를 선언한 후

    ref.increase(ref1); // increase를 호출했습니다.

    // increase 함수는 int[ ]을 전달 받아 각 값을 ++ 해줍니다. ++는 해당값에 +1을 하는 것과 같습니다.

    // 따라서 int [ ] ref1={100,800,1000}; 이 int [ ] ref1={101,801,1001}; 로 변경되어 출력됩니다.

    public void increase(int[ ] n) {

    for(int i=0; i<n.length; i++) {

    n[i]++;

    }

    }

    두번째는 질문의 의도를 잘 모르겠네요.


  • 안녕하세요

    increase 메소드에서는 아래와같이 배열인자를 ++ 해주고있네요.

    0번째 인덱스 배열값을 1증가

    1번째 인덱스 배열값을 1증가..

    public void increase(int[ ] n) {

    for(int i=0; i<n.length; i++) {

    n[i]++;

    }

    그래서 101,801, 1001 이 되는것입니다

    sum을 추가 하고싶으시면

    public void sum(int a, int b) {

    return a+b;

    }

    라고 클래스를만들고 sum을 호출하시면 될것같네요.


  • 안녕하세요.

    Increase는 array의 값을 1씩 증가시켜주는 역할을 합니다.

    그리고 sum은 sum = num1 + num2와 같이 해주시면 되구요

    array에 대해서는 아래와 같이 하시면 됩니다.

    public class SumOfArray { public static void main(String[] args) { //Initialize array int [] arr = new int [] {1, 2, 3, 4, 5}; int sum = 0; //Loop through the array to calculate sum of elements for (int i = 0; i < arr.length; i++) { sum = sum + arr[i]; } System.out.println("Sum of all the elements of an array: " + sum); } }


  • 안녕하세요.

    main 메소드의 선언한 "ref1" 와 increase 메소드의 parameter인 "n" 은 array type은 메모리를 참조해서 같이 사용하기 때문에 increase 에서 [100, 800, 1000] 배열의 값을 각각 1씩 plus 하고 있기 때문에 increase 를 호출 한 후에는 1이 추가되어 표시 됩니다.

    sum 계산식이 원하는게 맞는지 모르겠네요.

    public class CallByRef { public int computeSum(int num1, int num2) { return num1 + num2; } public static void main(String[] args) { CallByRef ref = new CallByRef(); int num1 = 100; int num2 = 150; int sum = ref.computeSum(num1, num2); System.out.println("sum : " + sum); } }