0

Im trying to make a class which will be kind of data base. Here is a code:

public class DataFrame {

public HashMap<String, ArrayList<?>> data;
public String[] nazwy;
public String[] typy;

public DataFrame(String[] _nazwy, String[] _typy) {

    data = new HashMap<String, ArrayList<?>>();
    nazwy = new String[_nazwy.length];
    typy = new String[_typy.length];

    for(int i = 0; i < _nazwy.length; i++) {
        nazwy[i] = _nazwy[i];
        typy[i] = _typy[i];
        ArrayList<?> kolumna = new ArrayList<>();
        data.put(_nazwy[i], kolumna);
    }
}

public int size() {
    return data.size();
}

public ArrayList get(String kolname) {
    return data.get(kolname);
}

public DataFrame get(String [] kols, boolean copy) { // shallow and deep
    int size = kols.length;
    String[] _typy = new String[size]; // naprawic typy
    int i = 0;
    int j = 0;
    for(String kol : kols) {
        if(kol == typy[i]) {
            _typy[j] += typy[i];
            j++;
        }
        i++;
    }
    if(copy) {
        DataFrame temp = new DataFrame(kols, _typy);
        for(String kol : kols) {
            if(data.containsKey(kol))
                temp.data.put(kol, data.get(kol));
        }
        return temp;
    }
    else {
        DataFrame temp = new DataFrame(kols, _typy);
        for(String kol : kols) {
            if(data.containsKey(kol))

                temp.data.clone();
        }
        return temp;
    }
}

}

I made constructor, size() and get(column). It's working fine(Ive tested it). I also tried to make a deep copy and I think its working, but I have no idea how to make a shallow one which will have only listed columns. I found clone() method but it's not working :(

public DataFrame get(String [] kols, boolean copy) { // shallow and deep
    int size = kols.length;
    String[] _typy = new String[size]; // naprawic typy
    int i = 0;
    int j = 0;
    for(String kol : kols) {
        if(kol == typy[i]) {
            _typy[j] += typy[i];
            j++;
        }
        i++;
    }
    if(copy) {
        DataFrame temp = new DataFrame(kols, _typy);
        for(String kol : kols) {
            temp.data.put(kol, data.get(kol));
        }
        return temp;
    }
    else {
        DataFrame temp = new DataFrame(kols, _typy);
        temp.data = data;
        Set<String> set = new HashSet<String>();
        for(int k=0; k<kols.length; ++k)
            set.add(kols[k]);
        temp.data.keySet().retainAll(set);
        return temp;
    }
}

i made it like this. When im checking objects it returns false in shallowcopy and true when doing deep

  • Perhaps I'm using the terms incorrectly, but a deep copy would be, e.g., a new `Collection` where each element in it is a new `DataFrame` object based upon a previous `DataFrame`. A shallow copy would be a new `Collection` that uses references to existing `DataFrame` objects. See [What is the difference between a deep copy and a shallow copy](https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy). – KevinO Oct 03 '18 at 18:29
  • you want to create a shallow copy of for example a list? just use this: new ArrayList(yourlist), send your list to constructor and It's create a shallow copy. – Amin Oct 03 '18 at 18:39

0 Answers0