是否存在针对OrderedDictionary键数组的单行解决方案?

时间:2019-07-15 17:15:26

标签: c#

我正在寻找一种单行解决方案来获取OrderedDictionary的键。通过使用以下方法,我已经有了一个可行的解决方案,但是是否可以通过使用LINQ来使用更少的代码来实现相同的目的?

private string[] GetKeys(OrderedDictionary orderedDictionary)
{
    List<string> keyList = new List<string>(orderedDictionary.Count);

    foreach (DictionaryEntry entry in orderedDictionary)
    {
        keyList.Add(entry.Key.ToString());
    }

    return keyList.ToArray();
}

2 个答案:

答案 0 :(得分:1)

您可以使用它来减少代码-

**/ import java.util.Scanner;
    //import static java.lang.System.*;
    public class Tester {
        public static void main(String [] args){
            String input;
            WatingRoomManagment b= new WatingRoomManagment();
            System.out.println("1 Array Based \n2 Linkedlist Based");
            String type=new Scanner(System.in).next();
            if(type.equals("1")){
                b.arrayQueue();
            }
            else if(type.equals("2")){
                b.ListQueue();
            }
                do
            {
                System.out.println("Hospital Wating Room Managment Service");  
                System.out.println("1. Add a new Patient");
                System.out.println("2. Serve a patient");
                System.out.println("3. Can Doctor go Home?");
                System.out.println("4. Cancel All appointment");
                System.out.println("5. Display all Patient Information");
                System.out.println("6. Quit");

                System.out.println("Please enter either 1 to 6: "); 
                input =(new Scanner(System.in).next());

                if (input.equals("1")){
                    System.out.println("Name: ");
                    String nam=new Scanner(System.in).next();
                    System.out.println("Age: ");
                    String age=new Scanner(System.in).next();
                    System.out.println("blood: ");
                    String blood=new Scanner(System.in).next();
                    paitent a = new paitent(nam,age,blood);
                    b.RegisterPatient(a);
                }
                else if (input.equals("2")){
                    b.serveapatient();
                }
                else if(input.equals("3")){
                    b.CanDoctorgoHome();
                }
                else if(input.equals("4")){
                    b.cancelAll();
                }
                else if (input.equals("5")){
                    b.showAllpatient();
                }
            }while(!input.equals("6"));
        }
    }
    class paitent {
        String name;
        int id;
        String age;
        String blood;
        paitent(String name,String age,String blood){
            this.name=name;
            this.age=age;
            this.blood=blood;
            this.id=idGen();
        }
        public String getName(){
            return name;
        }
        public String toString(){
            String s= id+" "+name+" age "+age+" Blood "+blood;
            return s;
        }


        public int idGen() {

            int id = this.name.hashCode() + this.blood.hashCode();
            int length = String.valueOf(id).length();
            int Max_Length = 5;
            if(String.valueOf(id).length()>Max_Length) 
            {
                id = (int) (id /Math.pow(10.0,length - Max_Length ));
            }
            return  id;
        }
    }
    class WatingRoomManagment {
        static Queue q1;
        void arrayQueue(){
            q1= new ArrayQueue();
        }
        void ListQueue(){
            q1= new ListQueue();
        }
        void RegisterPatient(paitent k){
            try{
                q1.enqueue(k);
            }
            catch(QueueOverflowException e){
                System.err.println("Queue Full! Cannot Enqueue\n"+e);
            }
        }
        void serveapatient(){

            try{
                q1.dequeue();
            }
            catch(QueueUnderflowException e){
                System.err.println("Queue Empty\n"+e);
            }
        }
        void showAllpatient(){
            Object [] names=q1.toArray();
            Object temp;
            for (int i = 0; i < names.length; i++){
                for (int j = i + 1; j < names.length-1; j++){
                    if(names[i].getName().compareTo(names[j].getName())>0){
                        temp = names[i];
                        names[i] = names[j];
                        names[j] = temp;
                   }
                }
            }
            System.out.println(q1.toString());
        }
        void CanDoctorgoHome(){
            if(q1.isEmpty())
                System.out.println("Yes. He can go now");
            else
                System.out.println("No.");
        }
        void cancelAll(){
            q1.cancel();
        }
    }
    class ArrayQueue implements Queue {
        int size=0;
        int rear=-1;
        int front=-1;

        Object[] a = new Object[10];

        public void enqueue(paitent k)throws QueueOverflowException{
            if(size==a.length){
                throw new QueueOverflowException();
            }
            if(size==0){
                a[0]=k;
                a[(rear+1)%a.length]=k;
                rear=(rear+1)%a.length;
                front=(front+1)%a.length;
                size++;
            }
            else{      
                a[(rear+1)%a.length]=k;
                rear=(rear+1)%a.length;
                size++;
            }
        }
        public void dequeue() throws QueueUnderflowException{
            if(size==0)
                throw new QueueUnderflowException();
            a[front%a.length]=null;
            front=(front+1)%a.length;
            size--;
        }
        public boolean isEmpty(){
            if(size==0)
                return true;
            else
                return false;
        }
        public Object[] toArray(){
            Object[] temp = new Object[size];
            int start=front;
            for(int i=0; i<temp.length ; i++){
                temp[i]=a[start];
                start=(start+1)%a.length;
            }
            return temp;
        }
        public String toString(){
            String p="";
            if(size==0){
                p=p+"No is here";
            }
            else{
                int st=front;
                for(int i=0 ; i<size ; i++){
                    p=p+" "+a[st]+"\n";
                    st=(st+1)%a.length;
                } 
            }
            return p;
        }
        public void cancel(){
            int st=front;
            for(int i=0 ; i<size ; i++){
                a[st]=null;
                st=(st+1)%a.length;
            }
            size=0;
        }
        public int size(){
            return size;
        }
    }
    class ListQueue implements Queue{
        int size;
        Node front;
        Node rear;


        public ListQueue(){
            size = 0;
            front = null;
            rear = null;
        }
        public void enqueue(paitent o) throws QueueOverflowException{
            Node temp=new Node(o,null);
            if(size==0){
                front=temp;
                rear=front;
                size++;
            }
            else if(size>0){
                rear.next=temp;
                rear=temp;
                size++;
            }
        }
         public void dequeue() throws QueueUnderflowException {
            if(size==0){
                throw new QueueUnderflowException();
            }
            Node mn = front;
            if(size==1){
                rear=null;
                front=null;
                size--;
            }
            if(size>0){
                front = front.next;
                size--;
            }
            mn.val= null;
            mn.next= null;
        }
         public boolean isEmpty(){
            if(size==0)
                return true;
            else
                return false;
        }
         public String toString(){
            if(size==0)
                return "Empty Queue";
            else{
                String s = "[ ";
                for(Node n = front; n!=null; n=n.next){
                    s = s + n.val+ " ";
                }
                return s + "]";
            }
        }
         public Object[] toArray() {
            Object a [] = new Object [size];
            int i =0;
            for(Node n = front; n!=null; n=n.next){
                a[i] = n.val;i++;
            }
            return a; 
        }
         public void cancel(){
             front=null;
             rear=null;
             size=0;
         }
         public int size(){
             return size;
         }
    }
    class Node{
        paitent val;
        Node next;

        public Node(paitent v, Node n){
            val = v;
            next = n;
        }
    }
    interface Queue { 
        public int size(); 
        public boolean isEmpty(); 
        public void enqueue(paitent k) throws QueueOverflowException; 
        public void dequeue() throws QueueUnderflowException;
        public String toString();
        public Object[] toArray();
        public void cancel();
    }
    class QueueOverflowException extends Exception{

    }
    class QueueUnderflowException extends Exception{
    }**/

答案 1 :(得分:0)

代码学徒是正确的,我应该看一下MSDN文档。 在此找到了以下代码,并且可以执行我想要的操作。

undefined