Autor Beitrag
LINUS19
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 156
Erhaltene Danke: 1

Windows 10, 7
Java(Eclipse)
BeitragVerfasst: Mi 11.07.18 17:41 
Hallo,
für ein Spiel benötige ich eine LinkedList. Ich hatte bisher folgendes,nur wird dann in der console undefiened ausgegeben:

ausblenden volle Höhe JavaScript-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
function Node(value) {
  var next =null;
  this.value=value;
  //this.next=null;
}

function LinkedList() {

   var first = null;
   var elements = 0;

   this.addFirst = function addFirst(value) {

     elements++;
     var newNode = new Node(value);
     newNode.next = this.first;
     this.first = newNode;
   }

   this.get = function get(value) {

    var currentNode = first;
          var tmp =0;

    while(elements < value) {

         if(value > elements - 1) {
           console.log("Fehler ungültiger Index");
         }

         tmp++;
         currentNode= currentNode.next;

            if(tmp==value) {
    
                return currentNode;
          }
     
               }
   }
}

var l = new LinkedList();
l.addFirst(3);
console.log(l.get(0));


Woran liegt es, dass wenn ich mir ein Element ausgeben möchte, dieser Fehler aauftritt?
Danke im Vorraus.

Moderiert von user profile iconChristian S.: Code- durch JS-Tags ersetzt


Zuletzt bearbeitet von LINUS19 am Mi 11.07.18 23:33, insgesamt 1-mal bearbeitet
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 11.07.18 20:01 
Hallo,

hast Du denn mal im Debugger geschaut, wie der Ablauf im Programm ist, auf welche Variabeln / Felder zugegriffen wird und welche Werte die haben?

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".

Für diesen Beitrag haben gedankt: LINUS19
LINUS19 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 156
Erhaltene Danke: 1

Windows 10, 7
Java(Eclipse)
BeitragVerfasst: Do 12.07.18 00:22 
Danke, habe es mir im Debugger angeschaut und ich denke es funktioniert soweit wenn ich Zahlen hinzufüge. Aber wenn ich jetzt einen Punkt hinzufüge und mir die Koordinaten davon ausgeben lassen möchte bekomme ich folgende Fehlermeldung :Uncaught TypeError: l.get(...).getX is not a function
Woran liegt das?

ausblenden volle Höhe JavaScript-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
function Point(x, y) {
  this.x=x;
  this.y=y;

  this.getX = function getX() {
    return this.x;
  }

  this.getY = function getY() {
    return this.y;
  }
}

function Node(point) {
  var next =null;
  this.point=point;
}

function LinkedList() {

   var first = null;
   var elements = 0;

   this.addFirst = function addFirst(point) {

     elements++;
     var newNode = new Node(point);
     newNode.next = first;
     first = newNode;
   }

    this.addLast = function addLast(point) {
      elements++;
      var newNode = new Node(point);
      var currentNode = first;

      while(currentNode.next!=null) {
        currentNode = currentNode.next;
      }
       currentNode.next = newNode;
       newNode = currentNode;
    }

   this.get = function get(index) {

    var currentNode = first;
    var tmp =0;

    while(true) {

         if(index> elements - 1) {
           console.log("Fehler: ungültiger Index");
            return null;
         }

         if(tmp==index) {
           return currentNode;
         }

         tmp++;
         currentNode= currentNode.next;
     }
   }
}

var l = new LinkedList();

l.addFirst(3);
l.addFirst(4);
l.addLast(5);
console.log(l.get(0));  // Als Ergebnis 4

Bei Punkt aber:

var p =new Point(2,3);

l.addFirst(p);
console.log(l.get(0).getX());  //Uncaught TypeError: l.get(...).getX is not a function
console.log(p.getX());   //  2


Moderiert von user profile iconChristian S.: Code- durch JS-Tags ersetzt
LINUS19 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 156
Erhaltene Danke: 1

Windows 10, 7
Java(Eclipse)
BeitragVerfasst: Do 12.07.18 20:47 
ausblenden Quelltext
1:
2:
var p =new Point(2,3);
console.log(p.getX());


So bekomme ich als Ausgabe 2.

Nur wenn ich
ausblenden Quelltext
1:
console.log(l.get(0).getX());					
schreibe kommt diese Fehlermeldung: Uncaught TypeError: l.get(...).getX is not a function

Eigentlich wird dabei ja ein Punkt Objekt zurückgegeben, warum tritt dann diese Fehlermeldung auf?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 12.07.18 22:07 
user profile iconLINUS19 hat folgendes geschrieben Zum zitierten Posting springen:
Eigentlich wird dabei ja ein Punkt Objekt zurückgegeben
Hast Du das im Debugger überprüft? Für mich sieht das nicht so aus.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".