Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Playing with Arrays

 
Stefan Jaeschke
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the declaration of a inside the method fillArray shadows the instance variable a.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the bad formatting above...

 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic