Array Last Index Of

From SwiftAPI

Jump to: navigation, search

Contents

C#

List<int> haystack = new List<int>() { 9, 3, 8, 5, 8, 7 };
int needle = 8;
int pos = haystack.LastIndexOf(needle);
// pos == 4

Java

for Lists:

import java.util.Arrays;
 
List<Integer> haystack = Arrays.asList( 9, 3, 8, 5, 8, 7 );
Integer needle = 8;
int pos = haystack.lastIndexOf(needle);
// pos == 4

for arrays of objects (not primitive arrays):

import java.util.Arrays;
 
Integer[] haystack1 = { 9, 3, 8, 5, 8, 7 };
List<Integer> haystack = Arrays.asList( haystack1 );
Integer needle = 8;
int pos = haystack.lastIndexOf(needle);
// pos == 4

JavaScript

JavaScript 1.6+ (Firefox 1.5+), Prototype

developer.mozilla.org prototype

var haystack = [9, 3, 8, 5, 8, 7];
var needle = 8;
var pos = haystack.lastIndexOf(needle);
// pos == 4

Ruby

haystack = [9, 3, 8, 5, 8, 7]
needle = 8
pos = haystack.rindex(needle)
# pos == 4
Personal tools