Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array to String Help

    I am making a program where I have to display a graph of number ranges. The numbers are randomly generated. Right now it makes an array of 100 numbers. I need to convert this array to a string. When I run this it just prints [I@5570984c

    Code:
    import java.util.Random;
    import java.util.Arrays;  
      
    public class NumberGraph 
    {
      public static void main(String[] args)
      {    
        int [] numbers = new int [101];    
        int num;    
        boolean numberIn = false;    
        int count;    
        
        for (int loop = 0; loop<numbers.length; loop++)
        { 
          numbers[loop]=-1;
          //System.out.println(numbers[loop]);
        }
        
        do
        { 
          num =(int)( Math.random()*101);
          count = 0;
          numberIn = false;      
          do
          {        
            if (numbers[count]==num)
              numberIn = true;        
            else if (numbers[count]==-1)
            {
              numbers[count]=num;
              numberIn = true;         
            }
            else           
              count++;
          }
          while(!numberIn);      
        }
        while(count!=100);
        
        numbers.toString();
        System.out.println(numbers);
        
      }
    }
      
        
        
      /**  for (int trip = 0; trip<numbers.length; trip++) 
          
          System.out.println(numbers[trip]);
        
        System.out.println("Total");
        System.out.println(count);
        
      }
    }
    */

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    .toString is typically linked to the object id regardless of what it is. I think most collections are actually the values, but not anything primitive like an array.
    You need to iterate it:
    PHP Code:
    for (int i numbers)
    {
        
    System.out.println(i);

    Or a standard for loop like you have.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •