Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, please why is the keyword "new" used in returning a new array in leetcode's question number 1 (twoSum).

Question - Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Brute Force Solution -
public class TwoSum {

    public static void main(String[] args) {

        int[] nums = {2, 5, 9, 7};

        twoSum(nums, 9);  
    }

    public static int[] twoSum(int[] nums, int target) {

        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    System.out.println("fisrtIndex is " + i + " secondIndex is  " + j);
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{};
      }
	}

My question is, why do we have to use the keyword "new" when returning a new integer array here(
return new int[]{i, j};
) when the class TwoSum doesn't extend the Arrays class in java?

How come we can create a new instance of an Array without extending the class Arrays in java?
The int in (return new int[] {i, j}) shows the return type but why use the keyword "new"?
I hope my question is clear, thanks for any help.

What I have tried:

public static int[] twoSum(int[] nums, int target) {

    for (int i = 0; i < nums.length; i++) {
        for (int j = i + 1; j < nums.length; j++) {
            if (nums[i] + nums[j] == target) {
                System.out.println("fisrtIndex is " + i + " secondIndex is  " + j);
                return new int[]{i, j};
            }
        }
    }
    return new int[]{};
  }
Posted
Updated 27-Aug-20 23:09pm
v2

The new statements creates a new object or fundamental type, in this case an array of integers. The reference to the array is then returned to the calling routine. In your sample you have:
Java
public static void main(String[] args) {
    int[] nums = {2, 5, 9, 7};
    twoSum(nums, 9);  
}

Which does nothing, since the call to twoSums does not capture the returned array. A correct code would be:
Java
int[] moreNums = twoSum(nums, 9);
// code to deal with the returned array
 
Share this answer
 
Comments
UT7 28-Aug-20 5:14am    
@Richard MacCutchan, thanks.
The call to twoSum method was to print out the indices - System.out.println("fisrtIndex is " + i + " secondIndex is " + j); but i get your point, thanks for your time.
Richard MacCutchan 28-Aug-20 5:35am    
Then why is it creating a new array which it returns to the caller?
UT7 28-Aug-20 5:52am    
oh, the question on leetcode asked that we return an int array with the indices but i wanted to see the output(indices) that's why i added the print statement and called twoSum method from the main method.
Richard MacCutchan 28-Aug-20 8:16am    
Well you are returning it from the function, but you need to capture it in main, as I suggest above.
UT7 28-Aug-20 8:33am    
Okay, noted. Thank you.
The 'short form' answer to the 'why' is that here
int[] nums = {2, 5, 9, 7};
an integer array is declared, memory allocated and initialised with 4 integer elements as shown, 2,5,7,9 .. You 'could' rewrite twoSum to declare an empty array eg
int[] tmpNums = new int[2];
which declares an integer array and allocates memory for two integers, then
tmpNums[0] = i;
tmpNums[1] = j;
return tmpNums;
.. but twoSum as it is doesn't declare an array 'up front' with which to return the required elements, so to return an array, it MUST use 'new' to create a new array with the required memory allocation, before it can be assigned to and returned
 
Share this answer
 
v2
Comments
UT7 28-Aug-20 0:12am    
@Garth, thanks for your answer, very clear, thank you. But what would be the reference to the new array in memory?
CPallini 28-Aug-20 2:55am    
5.
UT7 28-Aug-20 3:57am    
@Garth please could you clarify, if tmpNums is the address for the new array object in memory, is the address (tmpNums) assigned automatically by JVM? Thanks for your time.
Garth J Lancaster 28-Aug-20 7:15am    
I guess you can say 'automatically assigned' - in the sense you rarely/shouldnt have to worry about the details - of course, under the covers, it's a little more complex
1) when the JVM start's up, it requests a 'block' of memory from the operating system (the size of which can be modified/optimised)
2) any requests to 'new' for example are served from this block by the JVM
.... so unless you run out of memory, you usually don;t care too much, it just 'happens'
UT7 28-Aug-20 7:19am    
Okay, thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900