简体   繁体   English

如何从双端队列发送并将其拆分为两个列表?

[英]How to send from deque and split it into two lists?

Hello everyone I was given the assignment in which I have大家好,我接到了我的任务

  1. In main, you create a queue with generated Strings word genereting should be done in main在 main 中,您使用生成的字符串创建一个队列,单词生成应该在 main 中完成
  2. You pass it to executeTasks, they are split into the appropriate lists您将它传递给 executeTasks,它们被分成适当的列表

I have a problem with referencing things in code it is still confusing for me☹️我在代码中引用事物时遇到问题,这仍然让我感到困惑☹️

it was much easier on the numbers数字要容易得多

Write a program that in the ArrayDeque queue will put 50 objects storing strings (strings), consisting of the letter 'a' repeated a random number of times (repeat range: 1-50).编写一个程序,在 ArrayDeque 队列中放入 50 个存储字符串(strings)的对象,由字母 'a' 组成,重复随机次数(重复范围:1-50)。 Filling the object with the repeats of the letter 'a' can be done with a for loop.可以使用 for 循环用重复的字母“a”填充对象。 Part 2第2部分

Extend the program from the first part in such a way that you pass the created queue to the method of your class, which will separate the objects from the queue into two ArrayList collections.从第一部分扩展程序,将创建的队列传递给类的方法,这会将队列中的对象分成两个 ArrayList 集合。 One of them will hold objects with an even number of 'a' characters, the other one with an odd number.其中一个将包含偶数个“a”字符的对象,另一个将包含奇数个字符。

in general, I have done the task in a slightly different way, but I need to correct it一般来说,我完成任务的方式略有不同,但我需要纠正它

import java.util.*;                                                     
import java.lang.*;                                                     


class TaskManager {                                                     

List<String> executedTasks;
List<String> even;
List<String> odd;

// constructor
public TaskManager() {                                               
    executedTasks = new ArrayList<>();
    even = new ArrayList<>();
    odd = new ArrayList<>();

}                                                                    

//method serving the list of tasks
public void executeTasks(Deque<String> theQueue) {                   
    while (theQueue.size() > 0) {                                     
        String theTask = theQueue.poll();                              
        System.out.println("Processing the task: " + theTask);
        char c = 'a';
        for (int n = 0; n < 50; n++) {
            String result = "";
            int count = (char) (Math.random() * 50 + 1);

            for (int i = 0; i < count; i++) {
                result += c;
            }                                  
            System.out.println(result);
            if (result.length() % 2 == 0) {
                even.add(result);
            } else {
                odd.add(result);
            }
        executedTasks.add(theTask);


            System.out.println("\nExecuted total " + executedTasks.size() + " tasks\n");  


        }

    }

 }
}


/* Name of the class has to be "Main" only if the class is public. */
public class QueuesAndLoops {                                                  

    public static void main(String[] args) throws java.lang.Exception { 


        char c = 'a';


        Deque<String> taskQueue1 = new ArrayDeque<>();                    

        for (int n = 0; n < 1; n++) {                                       
            taskQueue1.offer("The first task number " + (n + 1));
            
        }


        TaskManager taskExecutor = new TaskManager();                     
        taskExecutor.executeTasks(taskQueue1);                            

        System.out.println("Parzyste:");
        System.out.println(taskExecutor.even);
        System.out.println("Nieparzyste:");
        System.out.println(taskExecutor.odd);
    }
}

and this code works but I have a problem to change it to the required version并且此代码有效,但我无法将其更改为所需的版本

no one replied but it was about reworking it in a similar way没有人回复,但这是关于以类似的方式对其进行改造

import java.util.*;
import java.lang.*;


class TaskManager {

List<String> even;
List<String> odd;

// constructor
public TaskManager() {
    even = new ArrayList<>();
    odd = new ArrayList<>();

}

//method serving the list of tasks
public void executeTasks(Deque<String> theQueue) {

        String task;
        while ((task = theQueue.poll()) != null) { 
            System.out.println(task);

           boolean isAdded = task.length() % 2 == 0 ? even.add(task) : odd.add(task);


        }
    }
}


public class QueuesAndLoops {

    public static void main(String[] args) throws java.lang.Exception {

        final int NUM_TASKS = 50;
        String chr = "a";
        Deque<String> taskQueue1 = new ArrayDeque<>();

        for (int i = 0; i < NUM_TASKS; i++) {
            taskQueue1.offer(chr.repeat((int) (Math.random() * 50 + 1)));
        }

        TaskManager taskExecutor = new TaskManager();
        taskExecutor.executeTasks(taskQueue1);

        System.out.println("Even:");
        System.out.println(taskExecutor.even);
        System.out.println("Odd:");
        System.out.println(taskExecutor.odd);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM