如何从Deque发送并将其分为两个列表?

发布于 2025-02-06 00:59:09 字数 2805 浏览 2 评论 0原文

大家

  1. 大家 词生成应该在主要的情况下将
  2. 其传递给executetasks,它们分为适当的列表,

我在代码中引用事物的问题有问题 对于我来说,这仍然令人困惑☹️

在数字上

写一个程序要容易得多,该程序在Arraydeque队列中会放置50个存储字符串的对象(字符串),由字母“ A”组成,重复了随机数量(重复范围:1:1 -50)。 可以用字母“ A”的重复填充对象。 第2部分

以将创建队列传递给类方法的方式将程序从第一部分扩展到第一部分,该方法将将对象与队列分为两个arraylist集合。 其中一个将持有具有均匀数的“ A”字符的对象,另一个字符具有奇数。

通常,我以略有不同的方式完成了任务,但是我需要纠正它

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);
    }
}

,并且该代码有效,但是我有一个问题将其更改为必需的版本

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
  2. You pass it to executeTasks, they are split into the appropriate lists

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).
Filling the object with the repeats of the letter 'a' can be done with a for loop.
Part 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.
One of them will hold objects with an even number of 'a' characters, the other one with an odd number.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2025-02-13 00:59:09

没有人回答,但这是关于以类似的方式重新加工

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文