public static int f (int []a, int []b,int []c)
    {
        final int N=a.length;
        int j, k=0, g=0, t=0 ;
        for(int i=0;i<N;i++)
        {
          for (j=0;j<N;j++)
           if (b[j]==a[i])
             break;
          if (j==N)
          {
            c[t]=a[i];
            if (g==0 || c[t]>k)
            {
                k=c[t];
                g=1;
            }

            t++;
        }


    }
    return k;

Dani AI

Generated

Brief expert note: 's routine collects elements from a that are not found in b and attempts to return the largest of those while writing them into c. The threads from , and others already point out where the original main and array initialization went wrong. The main gaps left unaddressed are: the inner loop must iterate over b.length (not a.length), c must be sized large enough (or grown dynamically), and the function should make the “no result” case explicit instead of silently returning 0. The example below shows a safe test harness and a small, robust helper that fills c, returns how many values were written, and prints a clear result.

import java.util.Arrays;

public class TestMain {
    public static void main(String[] args) {
        int[] a = {2, 2, -1, 5};
        int[] b = {2, 3};
        int[] c = new int[a.length];        // safe: at most a.length unique entries
        int filled = collectNotInB(a, b, c); // fills c[0..filled-1]
        if (filled == 0) {
            System.out.println("No elements from a are absent in b.");
        } else {
            System.out.println("Max = " + max(c, filled));
            System.out.println("c = " + Arrays.toString(Arrays.copyOf(c, filled)));
        }
    }

    public static int collectNotInB(int[] a, int[] b, int[] c) {
        int t = 0;
        for (int v : a) {
            boolean found = false;
            for (int w : b) if (w == v) { found = true; break; }
            if (!found && t < c.length) c[t++] = v;
        }
        return t;
    }

    private static int max(int[] arr, int len) {
        int m = arr[0];
        for (int i = 1; i < len; i++) if (arr[i] > m) m = arr[i];
        return m;
    }
}

Troubleshooting notes: use the exact public static void main(String[] args) entry, remember System is case-sensitive, and prefer int[] arr style as noted by . For dynamic results prefer ArrayList<Integer> or return both max and count (or an OptionalInt) so the “no-result” situation is explicit.

Recommended Answers

All 8 Replies

i need to test it.
i true to write main method and i cannt do this without errors
need help
tnx .

what errors are you getting?

for writing main:
see this

Here's a little example:

public class A {
    public static void main(String a[]){
        print("Hello World.");
    }

    public static void print(String str){
        System.out.println(str);
    }
}

You'll have to include your main function, and your own function into a class. In Java everything has to be an Object/class, not like in C++ where we could had classes, and other small functions (a hybrid language).

So, try to create a class in which you'll put the static function public static void main(String a[]) which is the starting point of the program.

i wrote some main :

public static void main (String []args){
int[] arry= new a[];
 a[] = {1,1,2,3,3};
 b[]= {1,2,2,3,3};
 c[4]= {0,0,0,0,0};
 system.out.println(k);
}

the error : array dimension missing

Hi,
The porblem is at line number 2
while initializing the array you must provide the array size.
hence modify the line 2 as below

 int[] arry=new int[5]; //5 is the array size and it can be any number   

also at line 7 -->

 system.out.println(k); // k is not defined anywhere

hence your code will not compile.

if you are trying to print the array then iterate the array and print each element of the array during iteration.

if you just print the array using its name then you will not see the elements inside the array.System just prints the array object.

as you are creating array in java, you have to also specify the size of it.
Also you have to define type of it like,int,string,double....

So modify it...and run it again..

int[] arry= new a[];

It's totally wrong.
The new operator from Java requires, when instantiating a new object, to have as it's right operands the constructor of the required object you want to instantiate, which, in your case, it's int ( and than size, if allocating an array).
But, you can also declare an array of ints in a C++ like thing:

int array[]={0, 1, 2, 3, 4, 5};

Presumably, that's what you were looking for.

public class A{
    public static void main(String arg[]){
        int array[]={1, 2, 3, 4, 5, 6};
        for (int i=0;i<5;i++)
            print(Integer.toString(array[i]));
    }

    public static void print(String a){
        System.out.println(a);
    }
}

A small point:
although the declaration syntax int arr[] is perfectly valid, most standards prefer the form int[] arr That's the form you will find throughout the Java API source code.
The reason is that arr is an array of ints, as implied by the second form. The first form seems to suggest that there is an array of arrs that is an int.

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.