File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Playing with Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Playing with Arrays" Watch "Playing with Arrays" New topic
Author

Playing with Arrays

Stefan Jaeschke
Greenhorn

Joined: Oct 03, 2006
Posts: 2
Hi experts,
i am playing a little bit around with arrays an I am stuck on this (my intention was to create some code as a framework for sorting experiments):



The Code works fine but I wanted to put some flexibility into it:



Filling the Array still works fine but unfortunatly the showMe-Method raises an Exception: java.lang.NullPointerException (the array is empty - isn't it?).

So how can someone help me? I am looking for a way to fill and use the array without declaring the array-length inside the code. Any ideas (Thanks in advance)?


Bye Stefan


-- Stefan<br /> <br />Bingen, Germany
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The problem is that the declaration of a inside the method fillArray shadows the instance variable a.
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
You declared the member variable 'a' here:

but you never assigned anything to the member variable. This code:

creates another variable called 'a' that is local to the fillArray() function. When the fillArray() function terminates, that local variable is destroyed. You used different code in your first example. Compare your two examples to see the difference.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I once asked my boss about arrays.

She told me to use vectors instead.

Heh..I kill me.

(Arrays == a raise)

Sorry. Wrong forum for my humour.

-Cameron McKenzie
Stefan Jaeschke
Greenhorn

Joined: Oct 03, 2006
Posts: 2
Hi,
Thank you for your help and please excuse my stupid question.

Unfortunatly I found no other way to solve my problem: I want to create two (or more) arrays with different lengths.

I could create the arrays inside the main, but that look much like old school non OO-Programming, right? Otherwise I could create a new class just for the array length:


I just can not believe that this is real OO ...

So - do you have any ideas? I hope there is an easy way I just can not see.

Thanks!

Bye Stefan
John Cebedo
Greenhorn

Joined: Sep 23, 2006
Posts: 11
your code confused me, why do you have the BadStyle class?

why not simply do:


public class MyNumbers {
private double[] a;

public MyNumbers(int x) {
a = new double[x];
for (int i = 0; i < a.length; i++)
a[i] = Math.random();
}

public void showMe() {...}
}

so then if you want an array of 100 numbers, you go:
MyNumbers n = new MyNumbers(100);
n.showMe();
John Cebedo
Greenhorn

Joined: Sep 23, 2006
Posts: 11
sorry for the bad formatting above...

Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi cowboys,

nice idea from John with the constructor taking the array length.
You would have two instances of the MyNumbers class, each with its own array of doubles, when you have to have two arrays, one with 100 and one with 1000 elements.

Another possibility is not to make the method void, but give a double [] as a return type for fillArray.





BTW, you can but shouldn't code like
double a[] = new double[100];
Use
double [] a = new double[100];
instead, only for better readability.




Yours,
Bu.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Playing with Arrays