根据项目属性的值在控制台上显示列表项目的最佳方法是什么?

时间:2015-04-26 16:06:41

标签: c# list properties console items

我有一个List,它包含一些具有2个字符串和2个int属性的对象。我希望能够根据其第一个属性的内容显示对象的所有4个属性。 例如:我想显示列表中第一个属性为“Mozart”的所有项目的所有数据。 提前致谢!

我有一个非常基本的类,它有4个属性,2个字符串和2个整数,所有这些都将各自的getter / setter设置为public。

我还有一个包含其中一些对象的List。

我的代码看起来像这样。

Console.WriteLine("Give in the name you want to search for!");
string s = Console.ReadLine();

在此之后,我想检查第一个属性是否为“s”,如果是,则在屏幕上显示所有给定对象的数据。

3 个答案:

答案 0 :(得分:1)

看看这个并告诉我你是否遇到过任何问题:)

public class RecViewAdapter extends
        RecyclerView.Adapter<RecViewAdapter.ViewHolder> {
    Context mContext;
    List<String> mRunns;
    static ExecutorService mExec;
    static HashSet<Integer> mProcessed = new HashSet<>();

    public RecViewAdapter(Context context, List<String> runns) {
        mContext = context;
        mRunns = runns;
        mExec = Executors.newFixedThreadPool(1);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.runnabel_item, viewGroup,
                false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.runnName.setText(mRunns.get(position));
        if (!mProcessed.contains(position)) {
            new ProgressTask(holder.pBar, position).executeOnExecutor(mExec, null);
            mProcessed.add(position);
        }
    }

    @Override
    public int getItemCount() {
        return mRunns.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView runnName;
        ProgressBar pBar;

        public ViewHolder(View view) {
            super(view);
            runnName = (TextView) view.findViewById(R.id.textView);
            pBar = (ProgressBar) view.findViewById(R.id.progressBar);
            pBar.setIndeterminate(false);
            pBar.setMax(100);
            pBar.setProgress(0);

        }
    }
}

enter image description here

答案 1 :(得分:0)

这样的事情可以解决问题:

class Datum
{
    public string Composer { get; set; }

    ///wharever other proerties you need

    public string DisplayOutput()
    {
        return this.Composer //+ however you want it displayed
    }
}


class Program
{
    static void Main(string[] args)
    {

        List<Datum> data = new List<Datum>();

        foreach (var outputLine in data.Where(d => d.Composer == "Mozart").Select(d=>d.DisplayOutput())
        {
            Console.WriteLine(outputLine);
        }

    }
}

答案 2 :(得分:0)

这应该是一种可行的方法:

class Composer
{
   public Composer( string lastName, string firstName, int year, int month )
   {
      LastName = lastName;
      FirstName = firstName;
      YearOfBirth = year;
      MonthOfBirth = month;
   }
   public string LastName { get; set; }
   public string FirstName { get; set; }
   public int YearOfBirth { get; set; }
   public int MonthOfBirth { get; set; }
   public override string ToString()
   {
       return string.Format( "{0} {1} {2} {3}", LastName, FirstName, YearOfBirth.ToString(), MonthOfBirth.ToString() );
   }
}


class Program
{
   private static new List<Composer> composerList = new List<Composer>();

   static void Main( string[] args )
   {
      composerList.Add( new Composer( "Mozart", "Wolfgang", 1756, 1 ) );
      composerList.Add( new Composer( "Vivaldi", "Antonio", 1678, 3 ) );

      Console.WriteLine( "Please enter a name you want to search for!" );
      string name = Console.ReadLine();
      ShowComposerData( name );
   }


   private static void ShowComposerData( string name )
   {
      foreach( Composer comp in composerList )
      {
         if( comp.LastName == name )
         {
            Console.WriteLine( comp.ToString() );
         }
      }
   }
}