我现在已经编程了很多,但我有一个问题.我要做的是实现一个具有与内置数据类型int []相同功能的类.
我提供了以下方法:
public ArrayOfInt(int size);
public int length();
public int get(int index);
public void put(int index, int value);
Run Code Online (Sandbox Code Playgroud)
主要方法是测试代码.
我几乎不知道从哪里开始.有人能帮助我吗?
编辑:这是我更新的代码.当我尝试使用d.put(0,1)时,我得到一个nullpointerexception错误,这是我现在感到困惑的.d.put(0,1)不应该定义索引和值整数吗?
public class ArrayofIntegers
{
private int[] arr;
public int ArrayOfInt(int size)
{
arr = new int[size];
return size;
}
public int length()
{
return arr.length;
}
public int get(int index)
{
return arr[index];
}
public void put(int index, int value)
{
arr[index]=value;
System.out.print(arr[index]=value);
}
public static void main(String[] args)
{
ArrayofIntegers d = new ArrayofIntegers();
d.put(0,1);
}
Run Code Online (Sandbox Code Playgroud)
}
public class PriorityQueue<E> {
private E[] array;
private int size;
private int front;
private int back;
private int numOfElements = 0;
private static int EMPTY = 0;
public <E> int insert(E input)
{
if (numOfElements + 1 <= size)
{
array[back] = input;
back++;
numOfElements++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我收到一个编译错误,说我无法将输入文件(类型为E)转换为类型E.为什么会这样?是因为它在技术上与E型不同吗?
我想要做的是创建一个 lambda 函数,该函数在 ec2 实例上调用两个命令。当我在 lambda 函数中运行此代码时遇到问题时,我从exports.handler() 方法中删除了代码,并在同一个 ec2 实例中的独立 node.js 文件中运行了代码,并且我能够使代码正常工作. 我运行的命令是“node app.js”。
exports.handler = async (event) => {
const AWS = require('aws-sdk')
AWS.config.update({region:'us-east-1'});
var ssm = new AWS.SSM();
var params = {
DocumentName: 'AWS-RunShellScript', /* required */
InstanceIds: ['i-xxxxxxxxxxxxxxxx'],
Parameters: {
'commands': [
'mkdir /home/ec2-user/testDirectory',
'php /home/ec2-user/helloWorld.php'
/* more items */
],
/* '<ParameterName>': ... */
}
};
ssm.sendCommand(params, function(err, data) {
if (err) {
console.log("ERROR!");
console.log(err, err.stack); // an error occurred
}
else {
console.log("SUCCESS!");
console.log(data);
} …Run Code Online (Sandbox Code Playgroud) 设计一种线性算法,该算法在所有这些子序列中具有最高总和的N个长整数序列中找到最多M个的连续子序列.实现您的算法,并确认其运行时间的增长顺序是线性的.
我已经阅读了几次,但我很难理解它想要我做什么.
我想要做的是让 eval 变量将每个字母放入堆栈然后打印出来。我收到 EmptyStackException 错误(假设这意味着堆栈中没有任何内容)。我不明白的是,我认为将 eval 字符串放入变量堆栈中。为什么是空的?
public static void main(String[] args)
{
Stack<String> variable = new Stack<String>();
String eval = StdIn.readString();
String alphabet = "abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < eval.length(); i++)
{
eval = eval.substring(i,i);
if (eval.equals(alphabet.substring(0, 52)))// checks if eval is equal to any letter of alphabet
{
variable.push(eval);
System.out.println(variable.pop());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用日食
示例运行:
input: hello
Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Unknown Source)
at java.util.Stack.pop(Unknown Source)
at eval.main(eval.java:31)
Run Code Online (Sandbox Code Playgroud) import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer.*;
class Timer {
public static void main(String[] args) {
JFrame frame = new JFrame();
final int FIELD_WIDTH = 20;
final JTextField textField = new JTextField(FIELD_WIDTH);
frame.setLayout(new FlowLayout());
frame.add(textField);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
Date now = new Date();
textField.setText(now.toString());
}
};
final int DELAY = 1000;
Timer t = new Timer();
t.start();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
这可能是一个语法错误,但我不这么认为,因为我直接从一本书中复制了这个程序.代码行't.start();' 在它下面有一个错误行,说start()方法是未定义的.起初,我认为start()方法不存在,但我在库中查找了它.
我正在做一个Simon说的游戏.我遇到的一个问题是,在第一级之后,我的颜色不会改变/亮起.我跟踪代码并包含print语句以确保代码也正常工作.
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class SimonShape extends JFrame implements KeyListener {
private int width;
private int height;
private int x;
private int y;
private int TURN = 45;
private int level = 1;
//speed of the light up sequence
private int lightUpSpd = 500;
//chooses random color based on numbers 0-3
private int random; …Run Code Online (Sandbox Code Playgroud) 我正在尝试FlashCard在CardSet班上创建一个对象.问题是我的FlashCard对象无法实例化.我的类都不是抽象的,也不是接口.
CardSet类的第19行发生错误:
this.add(new FlashCard(term,definition)); .
public class CardSet<FlashCard> extends LinkedList<FlashCard> {
private String nameOfSet;
private FlashCard activeCard;
private String term;
private String definition;
public CardSet(String nameOfSet)
{
this.nameOfSet = nameOfSet;
}
public void addCard(String term, String definition)
{
this.term = term;
this.definition = definition;
// cannot instantiate
this.add(new FlashCard(term, definition));
}
}
Run Code Online (Sandbox Code Playgroud)
public class FlashCard {
private String definition;
private String term;
public FlashCard(String term, String definition)
{
this.term = term;
this.defintion = definition; …Run Code Online (Sandbox Code Playgroud) 我正在制作一个Simon Says游戏,当我在KeyListener的按()方法中调用randomColorChange()方法时,它会让winow不会关闭.randomColorChange()方法随机更改颜色.方法完成后,我希望keyPressed()方法点亮键:
public void randomColorChange(){
decrement = simonIncrease;
try{
int random;
for (int i = 0; i < simonIncrease; i++)
{
random = (int) (Math.random() * 4);
Thread.sleep(lightUpSpd);
if (random == 0)
{
green = green.brighter();
repaint();
Thread.sleep(lightUpSpd);
green= green.darker();
repaint();
}
if (random == 1)
{
red = red.brighter();
repaint();
Thread.sleep(lightUpSpd);
red = red.darker();
repaint();
}
if (random == 2)
{
blue = blue.brighter();
repaint();
Thread.sleep(lightUpSpd);
blue = blue.darker();
repaint();
}
if (random == 3)
{
yellow = yellow.brighter(); …Run Code Online (Sandbox Code Playgroud) 在我的FlashCardPanel类中,我有一个子面板LabelPanel,带有网格包布局.它由带有编辑按钮的构造函数,用于"翻转"卡片的按钮和用于显示术语/定义的标签组成.我的问题是,每次单击"翻转"按钮以显示我的术语的定义时,翻转按钮将改变大小,通常与定义的长度相匹配.
问题的图像 http://postimg.org/gallery/ymww3axq/
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FlashCardPanel extends JPanel{
private String term;
private String definition;
// shows the current text whether it is a term or definition
private JLabel currentLabel;
private static String NO_CARDS = "This set is empty";
//current card being displayed
private FlashCard currentCard;
//new card that is added …Run Code Online (Sandbox Code Playgroud) public class Library{
private ArrayList<Book> books = new ArrayList<Book>();
public void addBook()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter title of the book: ");
String title = sc.nextLine();
System.out.println("Enter the author: ");
String author = sc.nextLine();
String bookID = bookID();
Book b = new Book(author,title,bookID,true);
books.add(b);
System.out.println( title + " by "+ author +" added to library.");
System.out.println("Book ID: " + bookID);
}
public void listBooks()
{
for (Book temp : books)
{
if (temp.loanStatus() == true) System.out.print("*");
System.out.println(temp.getTitle() + …Run Code Online (Sandbox Code Playgroud) 当我尝试运行脚本时,即使它在全局范围内定义,我的ONE,TWO和THREE变量也是未定义的.我的A,B和C变量被认为是定义的.起初我以为是因为我将常量值指定为键,但我没有在网上找到任何说我不能这样做的东西.
<?php class aClass
{
const A = 1;
const B = 2;
const C = 3;
const ONE = 1;
const TWO = 2;
const THREE = 3;
public $arr = [];
function __construct() {
$this->createArray();
}
function createArray() {
$this->arr[] = $this->A = [
$this->ONE => 'one.',
$this->TWO => 'two',
$this->THREE => 'three'
];
$this->arr[] = $this->B = [
$this->ONE => 'one',
$this->TWO => 'two',
$this->THREE => 'three',
];
$this->arr[] = $this->C = [
$this->ONE => 'one',
$this->TWO …Run Code Online (Sandbox Code Playgroud) (define function1 (lambda(val)
(if (list? val)
(function2 (val))
('!list))))
Run Code Online (Sandbox Code Playgroud)
当我尝试输入'(ttt)时,我收到以下错误:
application: not a procedure;
expected a procedure that can be applied to arguments
given: (t t t)
arguments...: [none]
Run Code Online (Sandbox Code Playgroud)
我已经定义了function2,它可以在我自己调用它时工作,但是我无法在function1中调用它.