• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Playing with Arrays

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that the declaration of a inside the method fillArray shadows the instance variable a.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for the bad formatting above...

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic