java - I need help passing an array and calling basic methods on it -


i'm new programming , java, , i'm trying write simple bubble sorting algorithm. might in bit on head; i'm not far along in oracle's java tutorials. trouble i'm having isn't bubble sorting itself, in creating array , printing before sorted.

here have far:

public class bubblesort {      public bubblesort(int size) {          // creates array         int[] items = new int[size];     }      public void fillarray(int[] a) {          // fill array random ints         (int i=0; i<(a.length-1); i++) {             a[i] = java.util.random.nextint(50);         }     }      public void printarray(int[] a) {          (int i=0; i<a.length; i++) {             system.out.print(a[i] + " ");         }     }      public void bubblesortalgorithm() {          // bubble sorting algorithm goes here      }      public static void main(string[] args) {          bubblesort bubblesort = new bubblesort(20);         bubblesort.fillarray(items);         bubblesort.printarray(items);         // bubblesort.bubblesortalgorithm(items);         // bubblesort.printarray(items);     } } 

i'm getting 3 compiler errors:

  1. non-static method nextint(int) cannot referenced static context

is because called in main method? how around that?

2.,3. compiler can't find symbol, items. items array of ints created in constructor class. need declare in main method?

i have feeling class structure off. again, i'm new. i'm new stackoverflow, i'm sorry if question isn't presented well.

you call:

java.util.random.nextint(50); 

it's preferred import classes you're going use. put block @ top of file:

import java.util.random; 

and change existing code to:

random.nextint(50); 

that fixes style problem, you're still going same compiler error.

static methods things belong class; don't need create (instantiate) object of class before using them. instead of every instantiation of class having method, instantiated instances of class share same static methods , variables.

specifically, .nextint() in random class not static; it's normal method. needs instantiated random work on. means should try:

random random = new random(); random.nextint(50); 

after you've instantiated random, can keep calling nextint() on it, many times you'd like.

one example of static methods commonly used in math class.

math.min(10, 5); math.max(100, 100000); 

and on.

the reason random needs instantiated has state. it's not random, pseudo-random, in needs number start with. if don't give number, takes current time. 2 java random objects initialized @ same moment... produce same series of "random" numbers.

this useful; can give them random object number start with, , can use behavior testing purposes.

random random = new random(1); 

that's seeding number 1. means it'll produce same random numbers nextint() every time run code. if don't give number seed, it's doing this, instead:

random random = new random(system.currenttimemillis()); 

but yeah; problem random.nextint isn't static method.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -