• Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning contents of an Array  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
am trying to assign an array of numbers from 1 to 10 using the code below. Basically am stuck on how to  return an array. Do I need a toString Method ?

 
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Benjamin,Welcome to Coderanch!
where exactly did you stuck.on which line?tell us some more about it?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you mean to print an array then you can use toString() method of Arrays class.though their are also other means to do it.
 
Benjamin Rollins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I want to create an object in the main program, then create another method which will sort the contents of the array results.

I have
 
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to  return an array

With a return statement:
 
Benjamin Rollins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



and in the main programme created a new object like this :


but getting an error ?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getting an error ?

Please copy the full text of the error message and paste it here. It has important info about the error.
 
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Rollins wrote:and in the main programme created a new object like this : 
In your program, you have created an object of Arrays1 in class but not in main method.

Array index always starts with 0 so in loop i better starts with 0
 
Benjamin Rollins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is after i create the new object like



i get this:
run:
java.lang.ClassFormatError: Method "<error>" in class arrays1/Arrays1 has illegal signature "()Lul/getValues;"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Exception in thread "main"

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the new code that compiles, executes and gives that error?  I've never seen that error message.

Try erasing the class file and recreating it.
 
Benjamin Rollins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think its got to do with my object creation for my constructor, but am not sure where am going wrong:

if i remove this object from the main program part i get no error;
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to say what the problem is without having code that can be compiled and executed for testing.
 
Benjamin Rollins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so i was being silly my object creation was outside the brackets of my method.

i have changed this though with the full code below:

i get no error but my



returns no values?

this is the corrected code:

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
returns no values?

How do you know? 
The code does not receive (by assigning to a variable)  what is returned by the method or try to print it to show what was returned,
 
Benjamin Rollins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of course i have to add a print output..




This is the output i got but not the array from 1 to 10 ...

run:
[I@1dd46f7
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the output i got but not the array from 1 to 10 ...

run:
[I@1dd46f7

That is the String returned by the toString() method of an int array.  If you want to see the contents of the array use the Arrays class's toString() method:
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Benjamin

The array contents are not printed because you are not calling the method which fills data into the array I.e add().  When you call getValues it oly prints the null array
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call getValues it oly prints the null array

The array is not null.  Arrays of primitives are filled with the primitive's default value.  For int the default  values would be 0s.
 
Benjamin Rollins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for the comment yes i found out the contents of the array are all zeros:

run:
Contents of the Array are[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

how do i modify my code to return values from 1 to 10...

this my code now below:


}
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i modify my code to return values from 1 to 10...

Change the code to assign those values to the array.
Using a loop would be the most common way.
 
Greenhorn
Posts: 4
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one may be helpful for you,

 
Benjamin Rollins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The closest i could get was this code below

but this gives me the output

Contents of the Array are[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

and not the 1 to 10?

 
Benjamin Rollins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok got it had to change the index from 1 to zero and add +1 to the i

so came with this
public int[] getValues(){
    for(int i=0; i<numbers.length; i++){
        
        numbers[i]= i+1;}
        return numbers;
    
    }
    
Thanks a lot for the guidance

cheers
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Rollins wrote:The closest i could get was this code below

but this gives me the output

Contents of the Array are[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

and not the 1 to 10?

You wrote:

Poorly formatted code is difficult to read. Let's format that properly so it's easier to read:

Now, keeping in mind that Java arrays start with index 0, walk through the loop. When i is 1, which element of the array gets set and what value is it set to? When i = 2, which element gets set and what is its value? When i = 3 ... and so on. Does that code ever set the 0th element of the array?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!