8

I'm trying to get all the items in a table view and place them in an array list for further processing. This is what I'm trying to achieve but obviously it won't work.

ArrayList<Consultation> showing = consultationTable.getItems();
8
  • 4
    If you must have an ArrayList, then ArrayList<Consultation> showing = new ArrayList<Consultation>(consultationTable.getItems()) Commented Oct 5, 2016 at 11:28
  • what about ArrayList<Consultation> showing = new ArrayList<>(consultationTable.getItems()); ? Commented Oct 5, 2016 at 11:28
  • 3
    Is there a reason why specifically need an ArrayList? Why not List<Consultation> showing = ...;? Program to an interface, not an implementation. Commented Oct 5, 2016 at 11:30
  • 1
    Why on earth do you need that? Unless you went out of your way to do otherwise, the ObservableList is backed by an ArrayList anyway (the default way to create one is ObservableList<Consultation> obsList = FXCollections.observableArrayList() or similar.). So what is wrong with List<Consultation> showing = consultationTable.getItems();. Is there really some ArrayList-specific API you need? Commented Oct 5, 2016 at 11:48
  • 1
    I wish that was the case, I didn't own the other class another developer created it and was using it for a database method. Too much would need to be changed on his end Commented Oct 8, 2016 at 3:55

4 Answers 4

9

Nice and recommended solution:

List<Consultation> showing = provider.getItems();

Solution to use only if necessary:

    List<Consultation> consultations = provider.getItems();
    ArrayList<Consultation> showing;
    if (consultations instanceof ArrayList<?>) {
        showing = (ArrayList<Consultation>) consultations;
    } else {
        showing = new ArrayList<>(consultations);
    }

If for some reason you need to use an ArrayList method that is not in the List or ObservableList interface (I cannot readily think of why), you may use the latter.

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

Comments

6

A simple method using Stream class

List<T> list = ObservableList<T>.stream().collect(Collectors.toList());

Comments

1

Try this

new ArrayList<>(consultationTable.getItems())

Comments

-2
// To convert an observable list to an array of strings given the following observable list:

// Create these in the class 
ListView<String> myListView = new ListView<>();
ObservableList<String> myList;

// Then define them in the start class myList = FXCollections.observableArrayList(someGivenArray); myListView.setItems(myList);

// Make an array to store list items in the observable list as strings
List<String> myArray = new ArrayList<String>();

// Loop through the observable list and load the string array
    for (int i =0; i<myList.size(); i++){
       myArray.add(myList.get(i));
    }
// Test by printing out to the screen or a text field
    System.out.println(myList.get(0));
    myTextField.setText(myList.get(0));

1 Comment

ObservableList is a List. Elements can be added from one list to another at construction time or using the addAll method.

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.