Jump to content

Java Array Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
cooldude3663

cooldude3663

    Newbie

  • Members
  • Pip
  • 4 posts
I need some help, I am trying to write some code to produce a random array with the lenght and size of the value being set by the user. The array is then used in different algorithm methods. E.g. sort, binary search etc etc. I know I need to use JOpane option. But I am clueless on what to do.

Thanks in advance.

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
You ask the value from the user via JOptionPane()

Help: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

Then you'll get the value and make new array based on the value.
Then, you will use for-loop to fill the array with random numbers, which can be generated using Math.random() ... (Math.random() gives you x, { 0 =< x < 1 })
So if you want to get random numbers between 0 and 3, you just multiply the Math.random() by 4. and Floor it, by using Math.floor();
Math.floor(Math.random()*4)

in case you want to raise the lower limit of random numbers, you add the limit, like this.
Math.floor(Math.random()*4) + 5

this gives you random integers between 5 and 8

#3
cooldude3663

cooldude3663

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks this has made things slightly clearer. However I am a java noob. What is flooring?

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Flooring is like rounding, but always to the floor..
5.1 -> 5
5.7 -> 5
91.6 -> 91
1.9 -> 1
etc.

ceiling is exactly the opposite, everything will be rounded up.

#5
cooldude3663

cooldude3663

    Newbie

  • Members
  • Pip
  • 4 posts
/**
This class sorts an array, using the selection sort algorithm
*/
public class InsertSort2
{
/**
Constructs a selection sorter.
@param anArray the array to sort.
*/
public InsertSort2(int[] anArray)
{
a = anArray;
}
/**
Sorts the array managed by this selection sorter.
*/
/**
* The class InsertionSort
*
* @author Jon Jackson
*/
public class InsertionSort
{
// instance variables
private int i, n, newElement, location;
private int[] list;

/**
* The main sort method.
* @param list - the list to be sorted
*/
public void sort(int[] list)
{
n = a.length;
// for all elements in the a (excluding the first one)
for (i = 1; i < n; i++)
{
// store element to be inserted in a variable
newElement = a[i];
location = i-1;
// find location in a where new element should be inserted
while (location >= 0 && a[location] > newElement)
{
// shift element up one place in a
a[location + 1] = a[location];
// look at next element down in a
location = location - 1;
}
// insert new element into a
a[location + 1] = newElement;
}
}
}
}

Edited by cooldude3663, 31 October 2009 - 07:41 AM.
New code


#6
cooldude3663

cooldude3663

    Newbie

  • Members
  • Pip
  • 4 posts
I have written the new code to use a set array for now. However it keeps saying; cannot find symbol variable - a

#7
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Please provide the code inside the [code] tags and correctly tabbed, so it could be read.