how is Array object different from other objects

JPG

I am confused by the way arrays work. If I pass an array reference to some method, then that method is able to change the index values previously stored in array. However, if do the same with, say, a wrapper class object then that method is not able to change the value. Here is the code:

public class TestArray {
    public static void main(String[] args) {
        ChangeValues ch=new ChangeValues();
        int intArr[]={1,2,3};
        Integer iWrapper=new Integer(123);
        ch.changeArray(intArr);
        for(int i:intArr){
            System.out.print(i);// o/p: 789
        }
        System.out.println("\n-----------------");
        ch.changeWrapper(iWrapper);
        System.out.println(iWrapper);// o/p: 123
    }
}

class ChangeValues{
    void changeArray(int i[]){
        i[0]=7;
        i[1]=8;
        i[2]=9;
    }
    void changeWrapper(Integer i){
        i=789;
    }
}

output:

789
-----------------
123

Why is it that an array is able to change and not wrapper object. can any one clear away my doubts? thanks.

Hot Licks

Instead of using the Integer wrapper, use something like this (note that auto-boxing will be broken, so it will have to be done manually):

public class MyInteger {
    private int intValue;
    public MyInteger(int val) {
        intValue = val;
    }
    public void setValue(int val) {
        intValue = val;
    }
    pubic int intValue() {
        return intValue;
    }
}

Then your changeWrapper method can be:

void changeWrapper(MyInteger i){
    i.setValue(789);
}

And

    ch.changeWrapper(iWrapper);
    System.out.println(iWrapper.intValue());

Will print "789".

Alternatively, you could make intValue public:

public int intValue;

And then your changeWrapper method could become:

void changeWrapper(MyInteger i){
    i.intValue = 789;
}

This is effectively what an array is -- an object with a whole bunch of public fields.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Get object from array with NSDictionary objects

분류에서Dev

Creating a composite object from two other core data objects

분류에서Dev

How to access attributes of php object inside a array of php objects from javascript

분류에서Dev

jmeter: I want to extract 2 values from 2 different json objects where 1 json object will validate my condition & other value I want to extract

분류에서Dev

Want to retrieve values not other details from array object in rally

분류에서Dev

How to flatten an array of objects into arrays for each object parameter?

분류에서Dev

How to display same property from different objects in Javascript

분류에서Dev

PHPDoc - Different objects with different class within an array

분류에서Dev

How to get object from array in swift

분류에서Dev

Changing object at NSMutable Array of Objects

분류에서Dev

Populating Object array with new Objects

분류에서Dev

NSPredicate SUBQUERY for object composed of arrays of other objects

분류에서Dev

How to bind a list of objects of a class to a datagridview whose one field is again an object of some other class?

분류에서Dev

how in angularjs we can achieve the object update which is based on some other objects?

분류에서Dev

How to return an array of object properties from a returned object of Laravel Eloquent

분류에서Dev

React / Spring Boot - how to populate array from list of objects

분류에서Dev

how to pass a value from a recursive function into an array in a different method

분류에서Dev

Adding the values from an object to an array of objects without overriding existing key values

분류에서Dev

How can I merge two complex JSON objects with only unique or different values only showing in resultant array

분류에서Dev

Remove object from array

분류에서Dev

How to access JavaScript object other members data whilst iterating over array in handlebars each statement

분류에서Dev

JSON object expecting an array of objects but gets nothing

분류에서Dev

Typescript literal object containing an array of objects

분류에서Dev

How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

분류에서Dev

How do I sort an array filled with objects, based on two values of the object?

분류에서Dev

How do I Use other functions from a different class in Asynchronous methods

분류에서Dev

Python. How to efficiently remove custom object from array

분류에서Dev

Check if SQL object is referenced by any other SQL objects

분류에서Dev

How to get array of objects from current collection and data from another collection in same query

Related 관련 기사

  1. 1

    Get object from array with NSDictionary objects

  2. 2

    Creating a composite object from two other core data objects

  3. 3

    How to access attributes of php object inside a array of php objects from javascript

  4. 4

    jmeter: I want to extract 2 values from 2 different json objects where 1 json object will validate my condition & other value I want to extract

  5. 5

    Want to retrieve values not other details from array object in rally

  6. 6

    How to flatten an array of objects into arrays for each object parameter?

  7. 7

    How to display same property from different objects in Javascript

  8. 8

    PHPDoc - Different objects with different class within an array

  9. 9

    How to get object from array in swift

  10. 10

    Changing object at NSMutable Array of Objects

  11. 11

    Populating Object array with new Objects

  12. 12

    NSPredicate SUBQUERY for object composed of arrays of other objects

  13. 13

    How to bind a list of objects of a class to a datagridview whose one field is again an object of some other class?

  14. 14

    how in angularjs we can achieve the object update which is based on some other objects?

  15. 15

    How to return an array of object properties from a returned object of Laravel Eloquent

  16. 16

    React / Spring Boot - how to populate array from list of objects

  17. 17

    how to pass a value from a recursive function into an array in a different method

  18. 18

    Adding the values from an object to an array of objects without overriding existing key values

  19. 19

    How can I merge two complex JSON objects with only unique or different values only showing in resultant array

  20. 20

    Remove object from array

  21. 21

    How to access JavaScript object other members data whilst iterating over array in handlebars each statement

  22. 22

    JSON object expecting an array of objects but gets nothing

  23. 23

    Typescript literal object containing an array of objects

  24. 24

    How to refer to each property of an object in an array of objects in MongoDB MapReduce JavaScript query?

  25. 25

    How do I sort an array filled with objects, based on two values of the object?

  26. 26

    How do I Use other functions from a different class in Asynchronous methods

  27. 27

    Python. How to efficiently remove custom object from array

  28. 28

    Check if SQL object is referenced by any other SQL objects

  29. 29

    How to get array of objects from current collection and data from another collection in same query

뜨겁다태그

보관