Finding element in java LinkedList

조회 수: 5 (최근 30일)
Jakub Matousek
Jakub Matousek 2016년 11월 16일
댓글: Jakub Matousek 2016년 11월 17일
Is there a way to get index of any vector+the vector, that has 1 on first index and 2 on second index? So in this case it would return [1 2 3 4] and 1. So I can remove it after, when for example last digit is 4...(Trying to implement DFS AI method for solving the decanting puzzle, could do it without linked list, but using linkedlist would make it more fun for me to write...)
open = java.util.LinkedList;
open.add([1 2 3 4]);
open.add([2 3 4 5]);
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2016년 11월 16일
Jakub - open is a built-in MATLAB function name, so you may want to rename your above variable to avoid conflicting with this function.
Also, please clarify what you mean by ...vector+the vector... Are you adding the two vectors? How does this example return both [1 2 3 4] and 1?
Jakub Matousek
Jakub Matousek 2016년 11월 16일
편집: Jakub Matousek 2016년 11월 16일
Maybe it would be better to just specify what I need: In DFS search I need to look if the created node already exist in the list, but i care only about the first three numbers. So I need the way to compare vectors(no matter last number) in linked list with normal matlab vector...also I thought it through and I dont think I actually need to know the index of the found vector. So I need to only compare vectors.

댓글을 달려면 로그인하십시오.

채택된 답변

Geoff Hayes
Geoff Hayes 2016년 11월 17일
Jakub - if you just want to compare each element in your linked list to a specific array, checking to see if the first n elements match those of the array, then you could try something like
myList = java.util.LinkedList;
myList.add([1 2 3 4]);
myList.add([2 3 4 5]);
myList.add([1 2 3 4 5 6 7 8]);
myList.add([1 2]);
myList.add([1 2 3]);
dataToCompare = [1 2 3]';
for k=0:myList.size() -1
element = myList.get(k);
if length(element) >= length(dataToCompare)
if all(element(1:length(dataToCompare)) == dataToCompare)
fprintf('Found a match at %d!\n',k);
end
end
end
The output for the above would be
Found a match at 0!
Found a match at 2!
Found a match at 4!
indicating a match on the [1 2 3].

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by