0

I'm trying to sort an array using Bubble sort, but it won't work. It says 'required: variable, found: value'

Array.java:143: error: unexpected type
temp.get(d) = temp.get(d+1);

^

required: variable
found: value


Array.java:144: error: unexpected type
temp.get(d+1) = swap;

^

required: variable
found: value



So the Main is given, and all I need to do is write this Bubble sort function


This is what I wrote

public static void bubbleSort(Array<Integer> lista){
boolean swapped;
    Array<Integer> temp;
    int n;

    n= temp.size;

    int swap;

     for (int c = 0; c < ( n - 1 ); c++) {
        for (int d = 0; d < n - c - 1; d++) {
            if (temp.get(d) > temp.get(d+1))
            {
                swap = temp.get(d);
                temp.get(d) = temp.get(d+1);
                temp.get(d+1) = swap;
            }
        }
     }
}



And here's the Main that was given

public static void main(String[] args) throws IOException{
    BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); 
    String s = stdin.readLine();
    int N = Integer.parseInt(s);
    Array<Integer> niza =new Array<Integer>(N);
    bubbleSort(niza);

    for(int i=0;i<N;i++){
        s = stdin.readLine();
        niza.set(i, Integer.parseInt(s));
    }

    System.out.println(brojDoProsek(niza));
}


How do I fix this error?

1
  • You can't assign to a function. You're looking for set(). Commented Jan 7, 2014 at 17:35

2 Answers 2

2

Assuming Array is in fact ArrayList, and assuming you want to store the value that is at index d + 1 in the index d, you need

temp.set(d, temp.get(d + 1));

temp.get(d) is not a variable to which you can assign a value. It's an expression returning a value. And you can't assign anything to a value.

Sign up to request clarification or add additional context in comments.

2 Comments

now there's a problem with n = temp.size; it says Array.java:134: error: variable temp might not have been initialized n= temp.size;
That's a good thing you get this error: you indeed haven't initialized temp. Do it.
0

As the other users have said, you have to use the set() method to change the value stored at an index of your array.

I think you meant to use an ArrayList. If that is the case, then you should be using n = temp.size() instead of n = temp.size to get the length of the list, as size is a method in the ArrayList class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.