This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kartoffelbrot
Kartoffelbrot wrote ...

2013/10/24

java.util.List

1
2
Kartoffelbrot Kartoffelbrot

2013/10/24

#
Dear programmers, how can I create a new List?
import greenfoot.*;
import java.io.Serializable;
import java.util.*;
/**
 * not finished
 */
public class Save implements Serializable
{
    private List<Object>files;
    private static final String obPath = "CCE_ProjectFiles_Objects",
    nmPath = "CCE_PojectFiles_Names";
    private String name;

    public Save(String name){
        this.name = name;
        files = new List();
    }

    public void addFile(Object file){
        files.add(file);
    }

    public List getFiles(){
        return files;
    }

    public static String getObjectFilePath(){
        return obPath;
    }

    public static String getNameFilePath(){
        return nmPath;
    }

    public void deleteFile(Object delete){
        files.remove(delete);
    }

    public void save(){
        Serialization.serialize(this, name);
    }
}
error: java.util.List is abstract; cannot be instantiated I know that it means, that I can't create an instance like default. But what does abstract mean and how do I have to handle it? German answers are welcome, too.
Kartoffelbrot Kartoffelbrot

2013/10/24

#
Ok I noticed, that List isn't serializable, but my question is still alive.
MatheMagician MatheMagician

2013/10/24

#
files = new ArrayList<Object>();
I think...
erdelf erdelf

2013/10/24

#
Also, soweit ich weiß gibt es keinen Konstruktor in der List class, daher kannste nicht einfach new List() benutzen, ich benutze einfach immer new ArrayList(), also änder einfach zeile 16 zu:
files = new ArrayList();
Kartoffelbrot Kartoffelbrot

2013/10/24

#
Thanks & Danke.
MatheMagician MatheMagician

2013/10/24

#
Oh, thanks Erdelf! I forgot about Object being the default. Anyway, I never use pure objects, so I always use "<>" to denote the class.
danpost danpost

2013/10/24

#
NVM.
Kartoffelbrot Kartoffelbrot

2013/10/24

#
Not it works fine. I replaced it by a twodimensional array. Now I get this error: java.io.InvalidClassException: Save; local class incompatible: stream classdesc serialVersionUID = -2211293735924197814, local class serialVersionUID = -8564104235973659010 What does it mean and what should I change?
MatheMagician MatheMagician

2013/10/25

#
Can you give us the new code?
Kartoffelbrot Kartoffelbrot

2013/10/25

#
import greenfoot.*;
import java.io.Serializable;
import java.util.*;
/**
 * not finished
 */
public class Save implements Serializable
{
    private Object[][] s;
    private static final String obPath = "CCE_ProjectFiles_Objects",
    nmPath = "CCE_PojectFiles_Names", path = "images/";
    private String name;

    public Save(String name){
        this.name = name;
        s = new Object[0][0];
    }

    public void addFile(Object[] file){
        Object[][] temp = new Object[s.length+1][0];
        for(int i = 0; i < s.length; i++)
            temp[i] = s[i];
        temp[temp.length-1] = file;
        s = temp;
    }

    public Object[][] getFiles(){
        return s;
    }

    public static String getObjectFilePath(){
        return obPath;
    }
    
    public static String getNameFilePath(){
        return nmPath;
    }
    
    public static String getPath(){
        return path;
    }

    public void deleteFile(int num){
        int plus = 0;
        Object[][] a = new Object[s.length-1][0];
        for(int i = 0; i<a.length; i++){
            if(i==num)
                plus=1;
            a[i]=s[i+plus];
        }
        s=a;
    }

    public void save(){
        removeInterfaces();
        Serialization.serialize(this, path+name);
    }

    private void removeInterfaces(){
        for(int y = 0; y < s.length; y++){
            for(int x = 0; x < s[y].length; x++){
                if(s[y][x] instanceof Interface)
                    s[y][x]=null;
            }
        }

        for(int y = 0; y < s.length; y++){
            for(int x = 0; x < s[y].length; x++){
                if(s[y][x] == null){
                    Object[] t = new Object[s[y].length-1];
                    int plus = 0;
                    for(int i = 0; i < t.length; i++){
                        if(i==x)
                            plus++;
                        t[i]=s[y][i+plus];
                    }
                    s[y]=t;
                }
            }
        }
    }
}
Kartoffelbrot Kartoffelbrot

2013/11/9

#
Help please :/
danpost danpost

2013/11/9

#
Line 16 sets the size of your array at zero by zero.
Kartoffelbrot Kartoffelbrot

2013/11/9

#
But that can't make this error: java.io.InvalidClassException: Save; local class incompatible: stream classdesc serialVersionUID = -2211293735924197814, local class serialVersionUID = -8564104235973659010 Furthermore I wanted it to be zero by zero because the objects should be added later, with the methods. But Thanks.
danpost danpost

2013/11/9

#
Once an array is created, the size is immutable. To add a new element to the array, you would have to copy the contents into another array, re-create the original array to make it larger, copy the contents back and then add the new element.
Kartoffelbrot Kartoffelbrot

2013/11/9

#
Kartoffelbrot wrote...
public void addFile(Object file){ Object temp = new Object; for(int i = 0; i < s.length; i++) temp = s; temp = file; s = temp; }
Isn't it this?
There are more replies on the next page.
1
2