Saving Arrays in Vector and get it out again

时间:2015-11-12 11:24:21

标签: java arrays list vector arraylist

I have the following case, i am sorry i cant paste my code in here.

My problem is:

I have 3 Classes SAVE, TRY, Main

in the try class i do:

I have a method called tryyourself

in this method i configure 6 numbers via Scanner and safe it into an array.

In the main i create a object from class try and use the method tryyourself

t.tryyourself

after i did this i want to save the full array in a list this list is created inside the class SAVE.

I use a vector as a list.

I use nameoflist.add(t) when i want to give it out it always get sth like t@151aj1

So i know my descriptions is very vague, and i will add the code this evening but can you tell me how i get the full array out of the vector? And can i display the full array on the console?

Edit:

Here is my code:

public class Main{

public static void main (String[] args) {
Try t = new Try();
Save s= new Save();

t.tryyourself();

s.saving(t);
s.showing();
}
}

Class Try:

public void tryyourself(){
    int a;
    int[] tries = new int[6];
    Random rnd = new Random();

    for(int i=0;i<=5;i++){
    a = rnd.nextInt(49)+ 1;
    tries[i]= a;
    }

    for(int i=0;i<=5;i++){
        System.out.print(tries[i] + "\t"  );
    }
    System.out.println();


}

Class: Save:

public class Save {

List<Try> saver = new Vector<Try>();

public void saving(try t){
    saver.addAll(Arrays.asList(t.getArray()));
    System.out.println(saver.get(0));
}

I definitely have problems saving an array and i also have problems getting it back out again. I have no idea how to solve my problem. And so far i didn't fully understand all of your answers, probably becasue of my limited english skills.

3 个答案:

答案 0 :(得分:1)

From your description you are trying to add the object 't' to the vector 'nameoflist'. Please note that the object 't' is not an array. It is an object of class TRY.

The method t.tryyourself will return an array which you should then add to nameoflist

Maybe you should be doing something like this

arr = t.tryyourself
nameoflist.add(arr)

See if this gives you proper results you are looking for. I am sorry that is the best i can come up with from your question. All the best.

答案 1 :(得分:0)

You print object TRY but not an array. You can override method toString() in class TRY like:

public String toString(){
    return Arrays.toString(array);
}

答案 2 :(得分:0)

If I understood you, then your problem is on this line of code:

nameoflist.add(t.getArray()) // getArray() is a covenience method to obtain the array.

there, what you are doing is adding the whole array as an element in the vector, so that, when you try to read its content, you should traverse the array. But I guess that you're trying to do is to add the whole array in a vector of numbers, not a vector of arrays. If that is the case, then try the code below:

nameoflist.addAll(Arrays.asList(t.getArray()));

All you have to do is to obtain the array from the "t" object and then add it to the vector. Then you can iterate the vector and get numbers, not arrays.

Hope it helps. ;)