0

Доброго времени! Собственно ошибка в этой части profiles = deserData("profiles")); Сразу после main

Выдает:

Incompatible types. Required: java.util.ArrayList Found: java.util.Objects

Я так понимаю нужно преобразовать Object в ArrayList?

package Cirilizacion;

import org.w3.x2000.x09.xmldsig.ObjectType;

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;    
import java.util.Objects;
import javax.swing.JOptionPane;

public class Main {

@SuppressWarnings("unchecked")
private static ArrayList<Profile> profiles = new ArrayList<Profile>();

public static void main(String[] args) {

    profiles = deserData("profiles"));
    Profile profile = new Profile();
    profile.setName(JOptionPane.showInputDialog(null, "Enter name"));
    profile.setName(JOptionPane.showInputDialog(null, "Enter sername"));
    profiles.add(profile);
    for (Profile profile1 : profiles) {
        System.out.println(profile1.getName() + " " + profile1.getSername());

    }
    System.out.println(profiles.size());
    serData("profiles", profiles);
}

private static Objects deserData(String file_name) {
    Objects retObject = null;
    try {
        FileInputStream fileInputStream = new FileInputStream(file_name + ".ser");
        ObjectInputStream InputStream = new ObjectInputStream(fileInputStream);
        retObject = (Objects) InputStream.readObject();
        fileInputStream.close();
        InputStream.close();
    } catch (FileNotFoundException e) {
        System.out.println("No file");
        System.exit(1);
    } catch (IOException e) {
        System.out.println("Error");
        System.exit(2);
    } catch (ClassNotFoundException e) {
        System.out.println("Error Class");
        System.exit(3);
    }
    return retObject;

}

private static void serData(String file_name, ArrayList<Profile> obj) {

    try {
        FileOutputStream fileOutputStream = new FileOutputStream(file_name + ".ser");
        ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream);
        outputStream.writeObject(obj);
        fileOutputStream.close();
        outputStream.close();
    } catch (FileNotFoundException e) {
        System.out.println("No file");
        System.exit(1);
    } catch (IOException e) {
        System.out.println("Error");
        System.exit(2);
    }

}


}

Class Profile

package Cirilizacion;

import java.io.Serializable;
import java.io.*;
import java.util.ArrayList;
import java.util.Objects;
import javax.swing.JOptionPane;

public class Profile implements Serializable {
private String name;
private String Sername;


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSername() {
    return Sername;
}

public void setSername(String sername) {
    Sername = sername;
}
}
3
  • укажите строчку ошибки 14 фев 2019 в 19:23
  • Нужно привести его к ArrayList
    – ezhov_da
    14 фев 2019 в 19:42
  • Ошибка вверху сразу после main public static void main(String[] args) { profiles = deserData("profiles")); 14 фев 2019 в 20:52

1 ответ 1

0

Попробуйте так привести к нужному типу:

    import java.util.ArrayList;
    import java.util.Arrays;

    public class ArrayListTest {
        public static void main(String[] args) {
            ArrayList<String> strings = new ArrayList<String>() {{
                add("a");
                add("b");
            }};

            ArrayList<String> listFromObject = (ArrayList<String>) get(strings);
            System.out.println(Arrays.toString(listFromObject.toArray()));
        }

        private static Object get(ArrayList<String> list) {
            return list;
        }
    }

И нужно использовать Object, а не Objects.

1

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.