0

I have no idea how to get my new list to print

import java.util.ArrayList;
import java.util.List;

  public class InsertionSort {
      public static int insertion(List<String> sorted) {

    sorted = new ArrayList<String>();
    String list[] = {"Banana","Pear","Apple","Peach","Orange"};

    String temp="";
    int f = list.length;
    for(int i=0;i<f;i++){
      for(int j=i+1;j<f;j++){
        if(list[i].compareToIgnoreCase(list[j])>0){
          temp = list[i];
          list[i]=list[j];
          list[j]=temp;
         }
      }
    }
    System.out.print(list);
    return list[].InsertionSort;

I keep getting this error for the line above 1 error found: InsertionSort.java [line: 22] Error: class expected

      }
  }
1
  • 2
    return list[].InsertionSort; is not a valid Java statement.... Commented Mar 20, 2013 at 1:16

2 Answers 2

1

You want to use the for-each loop, it will look like so :

    for ( String i : list){
       System.out.print(i);
    }

You can't print out the array like you did here:

  System.out.print(list); // DOES NOT WORK

Because println takes in a variety of parameters, but not an array( although one version takes an array of chars ). See the API

But if you said,...

  System.out.print(list[1]);

for example, it would compile..

You have other issues to fix though..

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

Comments

1
return list[].InsertionSort //what's mean is code?

if you want to print list you can like this:

for(String str:list) //this list is list<String>
{
System.out.println(str);
}

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.