2 Replies - 25769 Views - Last Post: 03 April 2012 - 03:23 PM Rate Topic: -----

#1 PinguTrace   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-March 12

Bubble Sort. ArrayList<String>

Posted 31 March 2012 - 02:16 PM

Hello I'm trying to use a bubble sort to sort an array list into alphabetical order. But I keep getting the error:

"SortString.java:20: error: unexpected type arr.get(j)=arr.get(i);

required:variable
found: value
SortString.java:21: error: unexpected type arr.get(i)=t;
required:variable
found: value"
With the ^ pointing at the first bracket before the j and the first bracket before the i.

I've tried writing the sort differently but I can't get it to work with the array list.

Here is my code

 import java.util.*;
class SortString {

	public static void main(String args[]){
	
	ArrayList<String> arr = new ArrayList<String>();
		arr.add("C");
		arr.add("D");
		arr.add("A");
		arr.add("R");
		arr.add("S");
		arr.add("O");
		
		for(int j=0;j<arr.size();j++){
			
			for(int i=j+1;i<arr.size();i++){
				if((arr.get(i)).compareToIgnoreCase(arr.get(j))<0){
					
					String t = arr.get(j);
					arr.get(j)=arr.get(i);
					arr.get(i)=t;
				}
			}
			
			
		System.out.println(arr.get(j));
		}
	}
}


Please Help

Is This A Good Question/Topic? 0
  • +

Replies To: Bubble Sort. ArrayList<String>

#2 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Bubble Sort. ArrayList<String>

Posted 31 March 2012 - 02:48 PM

You can't use the get() method to do an assignment as in
arr.get(j)=arr.get(i);

get() is a "getter", not a "setter". It can be used as an "r-value" meaning you can use in on the right hand side of =. It can't be used as an "l-value" on the left hand side of =.

For assignment, you can use the set() method, for example
arr.set( j, arr.get(i));



This will set the element at index j to the value that is found at index i.
Was This Post Helpful? 1
  • +
  • -

#3 PinguTrace   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 31-March 12

Re: Bubble Sort. ArrayList<String>

Posted 03 April 2012 - 03:23 PM

Thankyou so much!! Really appreciate it, and it's working now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1