my goal here is to create a new array from the old array according to a Criteria that i set (all numbers above 100 and even)..

however the program gives me 2 mistakes, and i dont know why?

public class Array {
    public static void main(String[] args){
    	Manipulate manipulate=new Manipulate(); //mistake here
    	
    	int[] numbers={123,213,1234,45,123,124,11,2,34};
    	manipulate.manipulate(numbers,new Data()); // mistake here
    	for (int i : numbers) {
			System.out.println(i);
    }
}

class Manipulate{
	public void manipulate(int[] values, Criterion function){ // does function refers first to the interface and then to the Data class when i call new Data()?
		for(int i=0;i<values.length;i++){
			values[i]=function.func(values[i]); // do those values go there after i call a method from class data ?
		}
		
	}
}

interface Criterion{

	int func(int num);
	
}

 class Data implements Criterion{
  
	@Override
	public int func(int num) {
		if (num>100 && num%2==0)
		return num;
		return num;
		
	}
 }	
}

No, what I just posted was not right. Looking again.

Okay, your two errors are, as best I can work it out, because Java is treating your inner classes as objects, and they're not static objects, so you can't reference them from main.

Myself, I had no idea that inner classes (the classes, not the objects) were treated as objects, but it makes a certain twisted sense.

Two fixes, both of them work. One, you can add "static" to the class declarations of Data and Manipulate:

static class Manipulate{ }
static class Data implements Criterion { }

This will get you running.
You could also move these classes (and the interfcae, Criterion) into their own files, this will also work.
Another thing that should work (haven't tried this one) would be to instantiate an Array object and make it call your inner classes. It won't be static, so it should be able to get at those.

Thanks for finding an interesting language weirdness that I hadn't come across before! (might be old hat to the inner class junkies, but I don't use 'em as much)

There are still some logic errors in your code, it doesn't do what you want it to do, but now at least you're able to debug, because your code will run.

Hi jon
An (non-static) inner class has access to the instance variables of its containing class, and so it must be accessed through an instance. Static inner classes have no access to instance variables.
http://download.oracle.com/javase/tutorial/java/javaOO/nested.html

the weird thing, is that this code works:
it is a similar code and doesnt use static.

public class DeleagateEx{
	public static void main(String[] args) {
		Manipulate manipulate=new Manipulate();
		
		int[] numbers={4,8,3,9,35,27,32,55,50};
		for (int i : numbers) {
			System.out.println(i);
		}
		System.out.println("===========");
		manipulate.manipulate(numbers, new DoZugi());
		for (int i : numbers) {
			System.out.println(i);
		}
	}
	
}

class Manipulate{
	public void manipulate(int[] values, Funcable myFunc) {
		for (int i = 0; i < values.length; i++) {
			values[i]=myFunc.func(values[i]);
		}
	}
}
interface Funcable{
	int func(int num);
}

class DoZugi implements Funcable {

	@Override
	public int func(int num) {
		if(num%2==0)
			return num;
		else 
			return num-1;
	}
	
}

why when you added static , the mistake dissappeared?
and why no new array is created? why the func method in data class doesnt work

Well, there you are. Haven't even had my coffee yet and I've already learned something new.

What is different about the second version is the placement of a single }
first version - Manipulate is defined inside Array, it's an inner class and defaults to non-static (see my last post)
second version - Maipulate is defined outside DeleagateEx so it's a top-level class which, by definition, is static

now i need the func method inside data class to categorize my data by the if condition.

why doesnt it work? why java doesnt weed out the numbers below 100 and that are uneven.

why cant i create a new array by that criteria?

func currently replaces odd numnbers by the next lower even number. What exactly do you want it to do?

The second code that i posted. calls for numbers out of the value array (numbers array to be exact) and evaluates them when the func method is called from Data class.

if a number is evaluated to be even, it puts it in an array and prints it..

#
values[i]=myFunc.func(values[i]);

it stores the even numbers there

#
for (int i : numbers) {
#
System.out.println(i);
#
}

and when i call to print them, they are being printed (all the even numbers only)


why the first code doesnt utilize the func function.. i set an if condition and it skips it

A quick remark on one line:

values[i]=myFunc.func(values[i]);

This will replace each member of values with the output of func() for that value. It sounds like your goal is to extract a set of members that have a certain characteristic - even parity - and create a subset of the original array. If this is correct, you need the index into your target array to be independent of the index into the source array, so that your target array ends up being continuous.

If you want to force the members of the source array to be even before placing them in the target array, then it's fine to use one index, since there will be a one to one relationship, but that's not the goal that you describe in your last posting.

i am trying to form a new array of even and larger than 100 numbers.. should look like that e.g.
int[] digits={200,190, 180, 888}

and weed out the numbers in the original numbers that dont conform the critieria

e.g. 2, 5, 67, 23, 43


how do i achieve this by having an independent array?
how can i make the func method inside the Data class do that job for me?

Okay, so this is a method which accepts an array of int[] and returns an array of int. The input is any int array, the output is the even numbers from that int array.

- You don't care what order they're in, specifically, you're not going to make an attempt to sort the output.
- You don't want to filter out duplicates.

So input {2, 3, 5, 6, 8, 34, 4, 4, 4}

should return {2, 6, 34, 4, 4, 4}

Correct?

so the method is:

public int[] filterEvens(int[] sourceArray) {}

right?

Now we don't know how big sourceArray will be, but we know we'll need an int array of that size to put our output in, so we have to declare that.

Then we have to go through the sourceArray and test each member. If it's even, we'll put it in the target array. Then we'll return the target array.
Remember that we have to keep track of two index variables. One is our location in the source array - that's what we're testing. The other is our location in the target array, that's where we're putting the even numbers. As soon as we see an odd number, those will be out of synch.

Go ahead, you take it from here.

where do i put that public int line...?
will it work/

That's the start of the method you're going to write to do this. It's just an ordinary method - it's public, it returns an int array, it's called filterEvens, and it takes an int array as a parameter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.