IAmBadAtThis
IAmBadAtThis

Reputation: 37

Insertion Sort using an array list java

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

      }
  }

Upvotes: 0

Views: 7123

Answers (2)

Railre
Railre

Reputation: 21

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);
}

Upvotes: 1

Caffeinated
Caffeinated

Reputation: 12484

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..

Upvotes: 1

Related Questions