- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can i refer an element of one array from another array in java?
Yes, you can −
int [] myArray1 = {23, 45, 78, 90, 10}; int [] myArray2 = {23, 45, myArray1[2], 90, 10};
But, once you do so the second array stores the reference of the value, not the reference of the whole array. For this reason, any updating in the array will not affect the referred value −
Example
import java.util.Arrays; public class RefferencingAnotherArray { public static void main(String args[]) { int [] myArray1 = {23, 45, 78, 90, 10}; int [] myArray2 = {23, 45, myArray1[2], 90, 10}; System.out.println("Contents of the 2nd array"); System.out.println(Arrays.toString(myArray2)); myArray1[2] = 2000; System.out.println("Contents of the 2nd array after updating ::"); System.out.println(Arrays.toString(myArray2)); System.out.println("Contents of the 1stnd array after updating ::"); System.out.println(Arrays.toString(myArray1)); } }
Output
Contents of the 2nd array [23, 45, 78, 90, 10] Contents of the 2nd array after updating :: [23, 45, 78, 90, 10] Contents of the 1stnd array after updating :: [23, 45, 2000, 90, 10]
- Related Articles
- How to move an array element from one array position to another in Java?
- How can we copy one array from another in Java
- Check One Array is Subset of Another Array in Java
- How to remove an element from an array in Java
- In PHP, how can I add an object element to an array?
- how can I declare an Object Array in Java?
- Maximum possible XOR of every element in an array with another array in C++
- How do I add an element to an array list in Java?
- Compute the truth value of an array XOR another array element-wise in Numpy
- How do you copy an element from one list to another in Java?
- Copy values from one array to another in Numpy
- How to print data of specific element from an array in java?
- Maximum in an array that can make another array sorted in C++
- Compute the truth value of an array AND to another array element-wise in Numpy
- Compute the truth value of an array OR to another array element-wise in Numpy

Advertisements