小编use*_*517的帖子

用于整数的Java ArrayList

我有值,我想添加到ArrayList中以跟踪显示的数字.值是整数,所以我创建了一个ArrayList;

ArrayList<Integer[]> list = new ArrayList<>();
int x = 5
list.add(x);
Run Code Online (Sandbox Code Playgroud)

但我无法使用此方法向ArrayList添加任何内容.如果我使用字符串作为数组列表,它的工作原理.我是否必须使它成为一个String数组,然后以某种方式将数组转换为整数?

编辑:我有另一个问题.我希望列表只能容纳3个值.我该怎么办?

java arrays

16
推荐指数
2
解决办法
17万
查看次数

平移列表中的所有元素

我被告知要

写一个函数square(a),它接受一个数组a,数字并返回一个包含每个平方值的数组.

起初,我有

def square(a):
    for i in a: print i**2
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为我正在打印,而不是像我被问到的那样回来.所以我试过了

    def square(a):
        for i in a: return i**2
Run Code Online (Sandbox Code Playgroud)

但这只是我阵列的最后一个数字.我怎样才能让它在整个列表中找到方位?

python arrays return

12
推荐指数
3
解决办法
9万
查看次数

单击后重命名按钮 - Java JButton

当我点击它时,我正试图将我的按钮从"开始"更改为"停止".我尝试这样做是在下面,我查了一下并试图复制指南,但我看不出我做错了什么.我可能会遗漏一些"}",因为我遗漏了许多不相关的代码.有人能看出我做错了吗?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PipeGameApp extends JFrame implements ActionListener {

    private static int BOARD_SIZE = 11;
    private PipeGame game;      // The model
    private PipeGameView view;      // The view

    // This constructor builds the window
    public PipeGameApp(String title) {
        super(title);

        game = new PipeGame(BOARD_SIZE);
        view = new PipeGameView(game);

        //THE TOP BAR
        JPanel topBar = new JPanel();
        JButton startButton = new JButton("Start");
        startButton.addActionListener(this);



        ButtonGroup bg1 = new ButtonGroup();
        JRadioButton rb1 = new JRadioButton("2 minutes", true); …
Run Code Online (Sandbox Code Playgroud)

java swing jframe jbutton actionlistener

7
推荐指数
1
解决办法
1万
查看次数

Java:按长度对单词列表进行排序,然后按字母顺序排序

有人告诉我有一个按长度排序的单词列表,而那些长度相同的单词按字母顺序排序。到目前为止,这就是我所拥有的方法。

public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
    TreeMap<String, Integer> s = new TreeMap<String, Integer>();
    ArrayList<Integer> count = new ArrayList<Integer>();
    String line;        
    int length;
    while ((line = r.readLine()) != null) {
        length = line.length();

        s.put(line, length);
        if (!count.contains(length)){
            count.add(length);
        }
    }    
    Collections.sort(count);
    System.out.println(count);
}
Run Code Online (Sandbox Code Playgroud)

我的想法是使用 TreeMap 来保留字符串,并以单词的长度作为键。我还有一个 ArrayList 可以跟踪所有单词的长度,没有任何重复,然后对其进行排序。

我希望以某种方式调用 TreeMap 的键值为 5,它会列出所有包含 5 个字母的单词。

我想知道我是否在正确的轨道上?我已经玩了一个多小时,似乎无法弄清楚在这之后我应该做什么。我是从正确的角度接近这个吗?

java sorting map

4
推荐指数
2
解决办法
2万
查看次数

初始化具有联合的结构数组的元素时遇到问题

我在初始化一个也有联合的结构时遇到了麻烦。我尝试遵循一些指南,似乎我是正确的,但如果它不起作用,显然不会。

我有以下标题

#ifndef MENU_H_
#define MENU_H_

typedef struct student{
    int gpa;
    float tuitionFees;
    int numCourses;
}student;

typedef struct employee{
    float salary;
    int serviceYears;
    int level;
}employee;

typedef struct person{
    char firstName[20];
    char familyName[20];
    char telephoneNum[10];
    int type; // 0 = student / 1 = employee;
    union{
        employee e;
        student s;
    };
}newPerson;

#endif
Run Code Online (Sandbox Code Playgroud)

然后这就是我遇到的麻烦

newPerson person[MAX_PERSONS];
person[1] = {"geo", "dude", "6136544565", 0, {3, 2353, 234}};
Run Code Online (Sandbox Code Playgroud)

当我尝试初始化 person[1] 时,出现以下错误

错误:'{' 标记前的预期表达式

我想知道这可能是什么原因?似乎我没有缺少大括号,我也尝试移除内部大括号,但它仍然不起作用。任何帮助将不胜感激。谢谢

c arrays struct initialization unions

2
推荐指数
1
解决办法
71
查看次数

从 Haskell 元组获取第一个值时遇到问题

我有一个以下形式的值([(Int, Int, Int)], Int, Int)

我需要一个以以下形式获取数据([(Int, Int, Int)], Int, Int)并返回的函数[[(Int, Int, Int)]]

假设我的列表名为it. 当我跑步时fst it

我收到这个错误

 * Couldn't match expected type `(a, b0)'
                  with actual type `([(Int, Int, Int)], Int, Int)'
    * In the first argument of `fst', namely `it'
      In the expression: fst it
      In an equation for `it': it = fst it
    * Relevant bindings include it :: a (bound at <interactive>:9:1)
Run Code Online (Sandbox Code Playgroud)

我很难理解我做错了什么。有人可以帮我吗。我想获取it其中的第一个值是列表的列表。

haskell functional-programming

2
推荐指数
1
解决办法
2224
查看次数

Python:从列表转换为字符串

我在做家庭作业问题时遇到了问题.

"编写一个函数to_str(a),它接受一个数组a,将其每个元素转换为一个字符串(使用str(a [i]))并将所有这些字符串附加在一起."

这就是我所拥有的

def to_str(a):
    for i in a: a.append([i])
    return str(a[i])
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用str(a [i]),我想知道是否有人可以指出我正确的方向

python string list

1
推荐指数
1
解决办法
3408
查看次数

在不同时间使用Python打印行

我正在做一个简单的家庭作业游戏.我想打印线条,但让它们彼此分开打印1秒钟.我该怎么做呢?

我认为会延迟打印的东西.所以喜欢

"Hello"
"My name is blahblah"
"This is blah blah"
"blah blah"
"What's your name?"
Run Code Online (Sandbox Code Playgroud)

python time

1
推荐指数
1
解决办法
78
查看次数

当它应该为真时,递归返回false

我正在制作一个匹配递归的单词,但是我遇到了一个问题.如果语句为true,我有一个if语句将返回true.我有一个system.print行来测试它是否真的正确运行它确实.但是,当该方法假设返回true时,它返回false.对不起,如果我不清楚,我希望我的代码清除它.

public class A10 {

    public static int counter = 3;

    public static boolean match(String x, String y) {
        // If the x string's letter at place 'counter' is the same as y string's letter at place counter.
        if ((counter) >= x.length()) {
            System.out.println("RUNNING THIS METHOD");
            return true;
        }

        if (x.charAt(counter) == y.charAt(counter)) {
            counter++;
            match(x, y);
        }

        return false;

    }

    public static void main(String[] args) {
        System.out.println(match("asdfasdf", "asdfasdf"));
    }
}
Run Code Online (Sandbox Code Playgroud)

当你运行它时,它会打印"运行这个方法",但是它会返回false,当它应该返回true时...有人可以告诉我是什么导致了这个以及我将如何修复它?

java if-statement boolean

1
推荐指数
1
解决办法
1920
查看次数

为什么我在Java中遇到stackoverflow?

我有一个int,"count"在每次递归后添加一个,但我也有一个if语句,一旦int等于或大于另一个整数就停止递归.不知何故if语句被忽略了.

public static boolean wildcard(String x, String y, int substring, int count) {
    if (count >= y.length()){
        System.out.println("asdf");
        return true;
    }

    if (x.charAt(count) == y.charAt(count)){
        System.out.println("ALSKDFJKL");
        return wildcard(x, y, substring, count++);
    }
    if (y.charAt(count) == '*'){
        return wildcard(x.substring(substring), y, substring++, count);


    System.out.println("wildcard end");
    return false;
    }
Run Code Online (Sandbox Code Playgroud)

java stack-overflow recursion

1
推荐指数
1
解决办法
47
查看次数