假设我们有
int a[2][3] ;
int (*p)[3]=a; // is ok
int (*p)[3]=&a[0]; // is also ok
Run Code Online (Sandbox Code Playgroud)
但为什么呢
int (*p)[3]=a[0];
Run Code Online (Sandbox Code Playgroud)
产生错误,虽然a[0]给出第一个数组的地址(因为2d数组是数组的数组)并且似乎比&a[0]给出第一个数组的第一个元素的地址还好,但为什么呢?
为什么fflush()不安全?我被告知它显示未定义的行为?如果是这样,fflush()的替代方法是什么?
如果我们写:
//in main
FILE *p = fopen("filename", "anymode");
Run Code Online (Sandbox Code Playgroud)
我的问题是:p指向什么?
在阅读线程中的并发问题并通过synchronized关键字处理它时,我想到的问题是,当我们使用术语时,lock它用于包含run方法(或线程的工作)的对象.但是为什么我们不能将该术语lock用于其定义包含synchronized关键字的方法,因为这个关键字意味着一旦线程进入该方法,那么该方法完成后该JVM调度程序只能干扰该线程?
我正在从第一个头java开始学习并且在那里写了"对象被锁定"的行,并且给出的理由再次以一种质疑的方式,即"如果我们有两个同步方法会发生什么".所以我在这里感到困惑如果只锁定方法会发生什么令人惊讶的事情呢?
如果我提出一个模糊的问题并提前致谢,请原谅我.
我正在尝试愚蠢的事情来使关于波动(我可以添加哪个组件)和继承的概念清楚.
JFrame f = new JFrame();
JFrame g = new JFrame();
f.add(g); // i know that its silly.
Run Code Online (Sandbox Code Playgroud)
它编译fin因为JFrame是a container并且add方法在其中定义.它的声明是
Component add ( Component comp);
Run Code Online (Sandbox Code Playgroud)
如果我做了上面有趣的东西我得到例外说
向容器添加窗口.
我从这里分析的是当我们调用继承树中上面的类中定义的方法时,该对象被视为继承树中上面的类的对象.当我们这样做时
f.add(g);
Run Code Online (Sandbox Code Playgroud)
f被视为容器因为add()未定义JFrame但在其父级(或更具体的父级)中定义的是Container.While g被视为Component,因为JFrame它Component也是.
这似乎是一个真实或错误的问题,但请从技术上解释我,我相信是对还是不对?
但为什么例外说
向容器添加窗口.
那么为何不
将容器添加到组件.
作为g传递给add()作为component?
我的第三个问题是,在Component和之间需要递归继承Container?
编辑为什么异常这样说?
看看下面的代码,其中两个方法抛出相同类型的异常但完全具有不同的上下文.
class Test
{
public void methodThrowingExceptionX()
{
//some code
throw new X();
}
public void anotherMethodThrowingExceptionX()
{
//some code
throw new X();
}
}
class TestClient
{
private Test testObj = new Test();
public Response TestMethodCaller()
{
try
{
testObj.methodThrowingExceptionX();
testObj.anotherMethodThrowingExceptionX();
}
catch(X xExceptionObj)
{
//this will catch X exception regardless from which method it is comming
//How can i distinguish from where the exception is coming(which method is throwing)
//so that i can return the Response object according …Run Code Online (Sandbox Code Playgroud) 我有以下一段代码
import java.io.*;
import java.util.*;
class MainDriver{
public static void main(String aa[]){
Scanner reader = new Scanner (System.in);
String name;
System.out.println("enter your name");
name = reader.nextLine();
name.trim();
System.out.println (name);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在输入中给出空格,我想知道输出
输入
bayant singh
Run Code Online (Sandbox Code Playgroud)
产量
bayant singh
Run Code Online (Sandbox Code Playgroud)
那么为什么name.trim;不在这里工作?因为我猜输出是
bayantsingh //no spaces
Run Code Online (Sandbox Code Playgroud) 我是主题的新手GridBagLayout,我无法理解约束weight和之间的确切区别fill.
我可以fill不指定weight.
除非您为weightx或weighty指定至少一个非零值,否则所有组件在其容器的中心聚集在一起.这是因为当权重为0.0(默认值)时,GridBagLayout会在其单元格网格与容器边缘之间放置任何额外空间.
我的问题是,如果这是真的那么为什么组件之间没有空间而且它们看起来是附加的?
我正在编写一个小游戏,其中我在JFrame中采用了JButtons网格,我想刷新JFrame中包含的按钮的颜色,这已经可见.如下所述
void foo(){
mainFrame.setVisible(true);//mainFrame is defined at class level.
//color update code for the buttons.
mainFrame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud)
我得到的结果不如预期,我的屏幕被冻结.这不是实现我想要的正确方法吗? 编辑 确定我正在详细解释我想要实现的目标.我有一个类,如: -
import javax.swing.*;
import java.awt.*;
import java.util.*;
class Brick extends JButton{
public void setRandomColors(){
int random = (int) (Math.random()*50);
if(random%13==0){
this.setBackground(Color.MAGENTA);
}
else if(random%10==0){
this.setBackground(Color.red);
}
else if(random%9==0){
this.setBackground(Color.yellow);
}
else if(random%7==0){
this.setBackground(Color.orange);
}
else if(random%2==0){
this.setBackground(Color.cyan);
}
else{
this.setBackground(Color.PINK);
}
}
public void setBlackColor(){
this.setBackground(Color.black);
}
}
class Grid {
JFrame mainGrid = new JFrame();
ArrayList<Brick> bunchOfBricks = new …Run Code Online (Sandbox Code Playgroud) 在一个递归方法来分组树的外部节点,我需要在该函数内动态分配内存.但是语句
static node* ext_node = malloc (sizeof(node));
Run Code Online (Sandbox Code Playgroud)
不起作用,编译器给出错误,其中说
初始化元素不是常量.
总之,我想问一下,static当动态获取指针指向的内存时,如何在递归调用中使用带有指针的关键字?
我需要这个,因为当需要在列表中添加更多元素时,该方法insert_to_end (node*)将负责为新节点分配存储,因此我可以创建list任何长度,这也是确切的内存要求.
但是如何用c语言实现这一目标呢?