我有一些Java枚举
public enum Aggregation
{
MORTGAGE( "Mortgage" ),
POOLS( "Pools" ),
PORTFOLIO( "Portfolio" );
private Aggregation( final String name )
{
m_Name = name;
}
private String m_Name;
static Map< String, Aggregation > c_LOOKUP =
new HashMap< String, Aggregation >();
static {
for (Aggregation agg:values()){
c_LOOKUP.put(agg.m_Name,agg);
}
}
public Aggregation lookup(String name){
return c_LOOKUP.get( name );
}
@Override
public String toString()
{
return m_Name;
}
}
public enum Interval
{
MONTHLY( "Monthly" ),
QUARTLY( "Quartly" ),
SEMIANNUALLY( "SemiAnnually" ),
ANNUALLY("Annually"); …Run Code Online (Sandbox Code Playgroud) 我试图从文件功能做一个简单的回放,似乎我的回调函数永远不会被调用.它并没有真正意义,因为所有OSStatus都返回0,其他数字也都显示正确(就像输出数据包从AudioFileReadPackets读取指针).
这是设置:
OSStatus stat;
stat = AudioFileOpenURL(
(CFURLRef)urlpath, kAudioFileReadPermission, 0, &aStreamData->aFile
);
UInt32 dsze = 0;
stat = AudioFileGetPropertyInfo(
aStreamData->aFile, kAudioFilePropertyDataFormat, &dsze, 0
);
stat = AudioFileGetProperty(
aStreamData->aFile, kAudioFilePropertyDataFormat, &dsze, &aStreamData->aDescription
);
stat = AudioQueueNewOutput(
&aStreamData->aDescription, bufferCallback, aStreamData, NULL, NULL, 0, &aStreamData->aQueue
);
aStreamData->pOffset = 0;
for(int i = 0; i < NUM_BUFFERS; i++) {
stat = AudioQueueAllocateBuffer(
aStreamData->aQueue, aStreamData->aDescription.mBytesPerPacket, &aStreamData->aBuffer[i]
);
bufferCallback(aStreamData, aStreamData->aQueue, aStreamData->aBuffer[i]);
}
stat = AudioQueuePrime(aStreamData->aQueue, 0, NULL);
stat = AudioQueueStart(aStreamData->aQueue, NULL);
Run Code Online (Sandbox Code Playgroud)
(未显示的是我正在检查stat函数之间的值,它只是恢复正常.)
和回调函数:
void …Run Code Online (Sandbox Code Playgroud) 这是我的isPrime方法:
private static boolean isPrime(int num) {
if (num % 2 == 0) return false;
for (int i = 3; i * i < num; i += 2)
if (num % i == 0) return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
我把isPrime(9)它放回去了true.这个方法有什么问题?
Java 8 Streams是否是公共方法的安全返回类型,因为在给定流的基础对象时不可能改变它?
例如,如果我有一个List并且return list.stream();可以以任何方式使用返回值来改变原始列表吗?
从API来看,我认为这不可能,但我想确认一下.
请向我解释一个lambda表达式如何使用和修改其封闭类的实例变量,但只能使用其封闭范围的局部变量.(除非是最终的或有效的决赛?)
我的基本问题是,在范围的上下文中,类的实例变量如何在lambda中可修改而局部变量不可修改.
如果声明byte或short类型的变量并尝试对这些变量执行算术运算,则会收到错误"Type mismatch:can int int int to short"(或相应的"Type mismatch:can int int to int").
byte a = 23;
byte b = 34;
byte c = a + b;
Run Code Online (Sandbox Code Playgroud)
在此示例中,编译错误位于第三行.
我正在尝试解决密码问题MissingInteger问题链接:
写一个函数:
Run Code Online (Sandbox Code Playgroud)class Solution { public int solution(int[] A); }如果给定N个整数的非空零索引数组A,则返回A中不出现的最小正整数.例如,给定:
Run Code Online (Sandbox Code Playgroud)A[0] = 1 A[1] = 3 A[2] = 6 A[3] = 4 A[4] = 1 A[5] = 2该函数应返回5.
假使,假设:
N是[1..100,000]范围内的整数; 数组A的每个元素都是[-2,147,483,648..2,147,483,647]范围内的整数.
复杂:
预期的最坏情况时间复杂度是O(N); 预期的最坏情况空间复杂度是O(N),超出输入存储(不计入输入参数所需的存储).可以修改输入数组的元素.
我的解决方案是:
class Solution {
TreeMap<Integer,Object> all = new TreeMap<Integer,Object>();
public int solution(int[] A) {
for(int i=0; i<A.length; i++)
all.put(i+1,new Object());
for(int i=0; i<A.length; i++)
if(all.containsKey(A[i]))
all.remove(A[i]);
Iterator notOccur = all.keySet().iterator();
if(notOccur.hasNext())
return (int)notOccur.next();
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
测试结果如下:

谁能解释我为什么我得到这两个错误的答案?特别是第一个,如果数组中只有一个元素,那么唯一正确的答案不应该是1吗?
在Java文档中,它提到使用f.setAccessible(true)方法我们可以违反封装的原则.
但是如果我正在编写任何具有完全安全性的类,例如使用私有变量,我怎么能防止它被反射访问?
例如,我有一个具有完整安全实例变量的类:
public final class Immutable {
private final int someVal;
public Immutable(int someVal) {
this.someVal = someVal;
}
public int getVal() {
return someVal;
}
}
Run Code Online (Sandbox Code Playgroud)
但我可以使用这样的反射修改该实例变量:
public class Tester {
public static void main(String[] args)
throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException {
Immutable i = new Immutable(10);
// output 10
System.out.println(i.getVal());
Field f = i.getClass().getDeclaredField("someVal");
f.setAccessible(true);
f.set(i, 11);
// output is 11 which implies some value modified
System.out.println(i.getVal());
}
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中,如何防止使用反射更改不可变类?
我正在上一堂课NameAndValue.我复制了一个对象数组,当我更改复制数组中的NameAndValue对象System.arrayCopy()时NameAndValue,它会反映在原始数组中.
public final class NameAndValue
{
public String name;
public String value;
public NameAndValue()
{
}
public NameAndValue(String name,String value)
{
this.name = name;
this.value = value;
}
}
public class Main
{
public static void main(String[] args)
{
NameAndValue[] nv = new NameAndValue[4];
nv[0] = new NameAndValue("A", "1");
nv[1] = new NameAndValue("B", "2");
nv[2] = new NameAndValue("C", "3");
nv[3] = new NameAndValue("D", "4");
NameAndValue[] nv2 = new NameAndValue[4];
System.arraycopy(nv, 0, nv2, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个简单的扫雷游戏,它现在可以正常工作,但我正在研究漂亮的细节,比如让每个数字变成不同的颜色.
当我尝试设置文本颜色时,我一直遇到错误JButton.我可以很容易地改变文本和背景,但不能特别改变文本颜色.
不断变得混乱的部分是:
total = Integer.toString(count);
jb.setText(total);
if(count == 1)
jb.setTextColor(Color.blue);
if(count == 2)
jb.setTextColor(Color.green);
if(count == 3)
jb.setTextColor(Color.red);
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我的错误是:
MS.java:109: error: cannot find symbol
jb.setTextColor(Color.blue);
^
symbol: method setTextColor(Color)
location: variable jb of type JButton
MS.java:112: error: cannot find symbol
jb.setTextColor(Color.green);
^
symbol: method setTextColor(Color)
location: variable jb of type JButton
MS.java:114: error: cannot find symbol
jb.setTextColor(Color.red);
^
symbol: method setTextColor(Color)
location: variable jb of type JButton
3 errors
Process javac exited with code 1Run Code Online (Sandbox Code Playgroud)
每当我尝试编译时都会发生这种情况,但是当我将其更改为说setBackgroundColor …
java ×9
arraycopy ×1
arrays ×1
audioqueue ×1
byte ×1
callback ×1
colors ×1
core-audio ×1
enums ×1
expression ×1
function ×1
inheritance ×1
java-8 ×1
java-stream ×1
jbutton ×1
lambda ×1
macos ×1
methods ×1
objective-c ×1
oop ×1
primes ×1
reflection ×1
scope ×1
swing ×1
variables ×1