选中数组中的复选框时更新标签

我正在尝试实现一种快速排序算法来对浮点数组进行排序。每当我引用数组中的索引时,我都会收到此错误:


无法将类型“float”隐式转换为“int”。存在显式转换(您是否缺少转换)?


这是算法:


class Quicksort

{

    public static void Sort(float[] numbers, int left, int right)

    {

        float i = Convert.ToSingle(left);

        float j = Convert.ToSingle(right);


        var pivot = numbers[(left + right) / 2];


        while (i <= j)

        {

            while (numbers[i] < pivot) //ERROR HERE

                i++;


            while (numbers[j] > pivot) //ERROR HERE

                j--;


            if (i <= j)

            {

                float tmp = numbers[i]; //ERROR HERE

                numbers[i] = numbers[j]; //ERROR HERE

                numbers[j] = tmp; //ERROR HERE


                i++;

                j--;

            }

        }


        if (left < j)

            Sort(numbers, left, j);


        if (i < right)

            Sort(numbers, i, right);

    }

}

numbers[i]每当我使用或时出现转换错误numbers[j]


我将如何解决这个问题?


繁花如伊
浏览 57回答 1
1回答

噜噜哒

我刚刚为你的场景写了一个例子,看看我认为它会有所帮助Xaml&nbsp; <StackPanel Grid.Row="0" Grid.Column="1">&nbsp; &nbsp; &nbsp; &nbsp; <ItemsControl ItemsSource="{Binding Path=CBItems}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ItemsControl.ItemsPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ItemsPanelTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel Orientation="Horizontal"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ItemsPanelTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ItemsControl.ItemsPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ItemsControl.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <CheckBox IsChecked="{Binding Path=CheckBoxChecked, Mode=TwoWay}" Command="{Binding Path=DataContext.CheckBoxChanged, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ItemsControl.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; </ItemsControl>&nbsp; &nbsp; &nbsp; &nbsp; <Label Content="{Binding LabelContent}"></Label>&nbsp; &nbsp; </StackPanel>Ctro(设置数据上下文)&nbsp;public partial class MainWindow : Window&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public MainWindow()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MainWindowVM DC = new MainWindowVM();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DC.Init();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.DataContext = DC;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }继电器命令和模型类&nbsp;class MainWindowVM : INotifyPropertyChanged&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public MainWindowVM()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CheckBoxChanged = new RelayCommand(CheckBoxChangedMethod);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private string labelContent="Not Yet Checked";&nbsp; &nbsp; &nbsp; &nbsp; public string LabelContent&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return labelContent; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { labelContent = value; OnPropertyChanged(new PropertyChangedEventArgs("LabelContent")); }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void Init()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CBItems = new ObservableCollection<ex>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 985; i <= 1030; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CBItems.Add(new ex { CheckBoxChecked = true });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public ICommand CheckBoxChanged { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; private ObservableCollection<ex> _CBItems;&nbsp; &nbsp; &nbsp; &nbsp; public ObservableCollection<ex> CBItems&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _CBItems; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _CBItems = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged(new PropertyChangedEventArgs("CBItems"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public event PropertyChangedEventHandler PropertyChanged;&nbsp; &nbsp; &nbsp; &nbsp; public void OnPropertyChanged(PropertyChangedEventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (PropertyChanged != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyChanged(this, e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void CheckBoxChangedMethod(object obj)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LabelContent = "You have Clicked the checkbox";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class RelayCommand : ICommand&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private Action<object> execute;&nbsp; &nbsp; &nbsp; &nbsp; private Func<object, bool> canExecute;&nbsp; &nbsp; &nbsp; &nbsp; public event EventHandler CanExecuteChanged&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CommandManager.RequerySuggested += value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remove { CommandManager.RequerySuggested -= value; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.execute = execute;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.canExecute = canExecute;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public bool CanExecute(object parameter)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //return this.canExecute == null || this.canExecute(parameter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void Execute(object parameter)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.execute(parameter);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public RelayCommand(Action<object> execute)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.execute = execute;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class ex&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private bool _checkBoxChecked;&nbsp; &nbsp; &nbsp; &nbsp; public bool CheckBoxChecked&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _checkBoxChecked; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { _checkBoxChecked = value; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP