2014-07-17 10 views
5

कहते हैं मैं इस सूची है: पिछले आइटम 9,: 1, 3, 5, 7, 9, 13एक सूची के किसी खास आइटम के पिछले/अगले आइटम जाओ <>

उदाहरण के लिए, दिए गए मूल्य है 7 है और अगला आइटम 13

मैं इसे सी # का उपयोग करके कैसे प्राप्त कर सकता हूं?

+2

हो सकता है दिए गए आइटम के सूचकांक प्राप्त करें। आगे बढ़ने के लिए 1 जोड़ें, पिछला स्थानांतरित करने के लिए एक घटाएं। सीमाओं की जांच सुनिश्चित करें। – DGibbs

+0

+/- 1 की अनुक्रमणिका .... आपने क्या प्रयास किया है? – Sayse

+0

किस तरह की सूची? यह एक (दोगुनी) लिंक्ड सूची के साथ बहुत आसान है, जैसे 'लिंक्डलिस्ट ' कक्षा। –

उत्तर

16

वांछित अनुक्रमणिका में तत्व प्राप्त करने के लिए आप सूचकांक का उपयोग कर सकते हैं। इंडेक्स में एक जोड़ना आपको अगले प्राप्त करेगा और इंडेक्स से एक को घटाकर आपको पिछले तत्व मिलेगा।

int index = 4; 
int prev = list[index-1]; 
int next = list[index+1]; 

आप अगले और पिछले सूचकांक अन्य मौजूद रहने पर वार आप IndexOutOfRangeException अपवाद मिल जाएगा जाँच करने के लिए होगा। सूची शून्य आधारित सूचकांक है इसलिए पहले तत्व में सूचकांक 0 होगा और दूसरा 1 होगा और इसी तरह।

if(index - 1 > -1) 
    prev = list[index-1]; 
if(index + 1 < list.Length) 
    next = list[index+1]; 
2
int index = list.IndexOf(9); // find the index of the given number 

// find the index of next and the previous number 
// by taking into account that 
// the given number might be the first or the last number in the list 
int prev = index > 0 ? index - 1 : -1; 

int next = index < list.Count - 1 ? index + 1 : -1; 

int nextItem, prevItem; 

// if indexes are valid then get the items using indexer 
// otherwise set them to a temporary value, 
// you can also use Nullable<int> instead 
nextItem = prev != -1 ? list[prev] : 0; 
prevItem = next != -1 ? list[next] : 0; 
+0

यह भ्रमित हो सकता है। यदि सूची का तत्व '0' है तो क्या होगा? फिर हम wheter निर्धारित नहीं कर सका सूची में कोई अगला/पिछला तत्व नहीं है या सिर्फ सूची में यह तत्व है। –

+0

@pwas हाँ मुझे लगता है कि Nullable का उपयोग करना एक बेहतर विचार होगा –

1
var index = list.IndexOf(9); 
if (index == -1) 
{ 
    return; // or exception - whater, no element found. 
} 

int? nextItem = null; //null means that there is no next element. 
if (index < list.Count - 1) 
{ 
    nextItem = list[index + 1]; 
} 

int? prevItem = null; 
if (index > 0) 
{ 
    prevItem = list[index - 1]; 
} 
3
 List<int> listInts = new List<int>(); 
     listInts.AddRange(new int[] { 1, 3, 5, 7, 9, 13 }); 
     int index = listInts.IndexOf(3); //The index here would be "1" 
     index++; //Check first if the index is in the length 
     int element = listInts[index]; //element = 5 
1

मैं नेट सूची

public class NavigationList<T> : List<T> 
    { 
     private int _currentIndex = 0; 
     public int CurrnetIndex 
     { 
      get 
      { 
       if (_currentIndex > Count - 1) { _currentIndex = Count - 1; } 
       if (_currentIndex < 0) { _currentIndex = 0; } 
       return _currentIndex; 
      } 
      set { _currentIndex = value; } 
     } 

     public T MoveNext 
     { 
      get { _currentIndex++; return this[CurrnetIndex]; } 
     } 

     public T MovePrevious 
     { 
      get { _currentIndex--; return this[CurrnetIndex]; } 
     } 

     public T Current 
     { 
      get { return this[CurrnetIndex]; } 
     } 
    } 

विरासत यह काफी आसान

NavigationList<string> n = new NavigationList<string>(); 
      n.Add("A"); 
      n.Add("B"); 
      n.Add("C"); 
      n.Add("D"); 
      Assert.AreEqual(n.Current, "A"); 
      Assert.AreEqual(n.MoveNext, "B"); 
      Assert.AreEqual(n.MovePrevious, "A"); 
0

यह करने के लिए हो जाता है का उपयोग करके इस को लागू किया है परिपत्र सूची का एक प्रकार है, इस प्रयास करें:

public class NavigationList<T> : List<T> 
{ 
    private int _currentIndex = -1; 
    public int CurrnetIndex 
    { 
     get 
     { 
      if (_currentIndex == Count) 
       _currentIndex = 0; 
      else if (_currentIndex > Count - 1) 
       _currentIndex = Count - 1; 
      else if (_currentIndex < 0) 
       _currentIndex = 0; 


      return _currentIndex; 
     } 

     set { _currentIndex = value; } 
    } 

    public T MoveNext 
    { 
     get { _currentIndex++; return this[CurrnetIndex]; } 
    } 

    public T Current 
    { 
     get { return this[CurrnetIndex]; } 
    } 
} 
0

के बाद सहायक

int NextValue = 0; 
int PreviousValue =0; 
int index = lstOfNo.FindIndex(nd =>nd.Id == 9); 

var Next = lstOfNo.ElementAtOrDefault(index + 1); 
var Previous = lstOfNo.ElementAtOrDefault(index - 1); 

if (Next != null) 
    NextValue = Next; 


if (Previous != null) 
    PreviousValue = Previous; 
संबंधित मुद्दे