0

I am trying to solve a popular problem on leetcode called two sum on my ide(intellij). I am expecting it to print index of values inside the array but its printing a memory address. I did some searching and found that i need to override to string(), i did the override and its still printing the memory address. what am i doing wrong? If anyone can help me out, i would appreciate it. Thank you. :)

import java.util.HashMap;
import java.util.Map;

public class Main {

    @Override
    public String toString() {
        return super.toString();
    }

    public static void main(String[] args) {
        int arr[]={2,7,11,15};
        int target= 9;
        int result[]=(find(arr,target));
        System.out.println(result);
    }

    public static int[] find(int[] arr,int target){
        Map<Integer,Integer> hm =new HashMap<>();

        for(int i=0;i<arr.length;i++){
            int num= target- arr[i];
            if(hm.containsKey(num)){
                return new int[]{hm.get(num),i};
            }
            hm.put(arr[i],i);
        }
        return new int[]{-1, -1};
    }
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
vaish
  • 31
  • 5
  • 4
    "*... its printing a memory address*" - Those are not memory addresses, but hash values --- "*...i did the override...*" - Yes, but the only thing you do is calling `return super.toString();`, which just returns the result of the `toString()`-method ob `Object`. --- Try using `System.out.println(Arrays.toString(result));` --- A remark: In Java, the array brackets are normally written after the type, not the variable name (`int result[] = ...` -> `int[] result = ...`) – Turing85 Aug 10 '20 at 22:21
  • Thank u so much.. Arrays.toString(result) worked...can u explain me why it was printing hash value instead of array index? thank you.. – vaish Aug 10 '20 at 22:28
  • 2
    This is the behaviour of [`Object::toString`](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Object.html#toString()), and since [arrays are objects](https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html), this is, what is called when an array is printed out. – Turing85 Aug 10 '20 at 22:29
  • Where does this fiction about "memory addresses" come from? Anyway, the lesson is surely that if you care about how your object's value is converted to a string, you have to write code to convert your object to a string. – user13784117 Aug 10 '20 at 22:59
  • yes thank you johannes and turing85. – vaish Aug 10 '20 at 23:01

2 Answers2

0

So there are two ways you can do this. First of all, the output that you are getting is basically hash values. What you want to do is print out the elements in that array. The first way to do it is to iterate through your array and simply print out the values like so:

for(int j=0;j<result.length;j++){
    System.out.println(result[j]);
}

The other way you could do this would be if you just used The Array class and its toString() method.

System.out.println(Arrays.toString(yourArray));

Both methods should work.

0

Your problem – which has been already answered – set aside, if you'd want to set up an environment in a single file, this might work OK for a Main.java file:

import java.util.*;
// import javafx.util.*;

class Solution {
    public static final int[] twoSum(int[] nums, int target) {
        int[] indices = new int[2];
        HashMap<Integer, Integer> map = new HashMap<>();

        for (int index = 0; index < nums.length; index++) {
            if (map.get(target - nums[index]) != null) {
                indices[1] = index;
                indices[0] = map.get(target - nums[index]);
                return indices;
            }

            map.put(nums[index], index);
        }

        return indices;
    }
}

class Main {
    public static void main(String[]args) {
        Solution s = new Solution();
        int arr[] = {2,7,11,15};
        int target = 9;
        System.out.println(s.twoSum(arr, target)[0]);
        System.out.println(s.twoSum(arr, target)[1]);
    }
}

Prints:

0
1
Emma
  • 27,428
  • 11
  • 44
  • 69