0

i am new to java programming language. I am storing objects to DoublyLinkedList data structure using my own implementation not the standard java library. I am trying to swap object elements not the Node. Is it possible?

Deck Class

DoublyLinkedList<Card> card = new DoublyLinkedList<>();

String[] ranks = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Joker", "Queen", "King"};
String[] suits = {"Club", "Spade", "Diamond", "Heart"};

public Deck() {
    for (String suit : suits) {
        for (String rank : ranks) {
            this.card.insertFirst(new Card(suit, rank));

        }
    }

}

public DoublyLinkedList<Card> getDeck() {
    return card;
}

public void swap() {
    int index1, index2;
    Random generator = new Random();
    Card temp;
    for (int i = 0; i < 4; i++) {
        index1 = generator.nextInt(card.size() - 1);
        index2 = generator.nextInt(card.size() - 1);
        //swap here
    }
}
  • 1
    __I am trying to swap object elements not the Node__. Please explain this line a bit more. Do you just wanted to swap the contents of the two Nodes, instead of the Nodes themselves !!!! And how is this Node represented? What is it's structure? – nIcE cOw Jun 12 '16 at 08:00
  • Yes, i just want to swap the contents of the two Nodes. – Clark Kent Jun 12 '16 at 08:03
  • 1
    Tell us what you already tried and what is not working. Please read http://stackoverflow.com/help/how-to-ask – Alexandre Cartapanis Jun 12 '16 at 08:04
  • 1
    First you need to get the 2 references to target nodes instead of indices. Then swap the values of the 2 nodes. – waltersu Jun 12 '16 at 08:13
  • @ClarkKent: I still don't understand, the actual question. Though, do try to search for [Copy Constructor](http://stackoverflow.com/q/869033/1057230). Seems like this is what you trying to get at :-) – nIcE cOw Jun 12 '16 at 08:47
  • I just want to swap the data of the nodes in a random fashion. @nIcEcOw – Clark Kent Jun 12 '16 at 11:14

0 Answers0