小编Old*_*ool的帖子

如何给指针赋予2d数组的基址

假设我们有

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]给出第一个数组的第一个元素的地址还好,但为什么呢?

c arrays

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

为什么fflush()不被认为是安全的?

为什么fflush()不安全?我被告知它显示未定义的行为?如果是这样,fflush()的替代方法是什么?

c file

3
推荐指数
2
解决办法
571
查看次数

C中的FILE指针如何工作?

如果我们写:

//in main

FILE *p = fopen("filename", "anymode");
Run Code Online (Sandbox Code Playgroud)

我的问题是:p指向什么?

c file

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

当我们使用synchronized关键字时,会被锁定什么?

在阅读线程中的并发问题并通过synchronized关键字处理它时,我想到的问题是,当我们使用术语时,lock它用于包含run方法(或线程的工作)的对象.但是为什么我们不能将该术语lock用于其定义包含synchronized关键字的方法,因为这个关键字意味着一旦线程进入该方法,那么该方法完成后该JVM调度程序只能干扰该线程?

我正在从第一个头java开始学习并且在那里写了"对象被锁定"的行,并且给出的理由再次以一种质疑的方式,即"如果我们有两个同步方法会发生什么".所以我在这里感到困惑如果只锁定方法会发生什么令人惊讶的事情呢?

如果我提出一个模糊的问题并提前致谢,请原谅我.

java multithreading synchronized

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

调用对象超类的方法时如何处理对象?

我正在尝试愚蠢的事情来使关于波动(我可以添加哪个组件)和继承的概念清楚.

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,因为JFrameComponent也是.

这似乎是一个真实或错误的问题,但请从技术上解释我,我相信是对还是不对?

但为什么例外说

向容器添加窗口.

那么为何不

将容器添加到组件.

作为g传递给add()作为component

我的第三个问题是,在Component和之间需要递归继承Container

编辑为什么异常这样说?

java inheritance swing

3
推荐指数
2
解决办法
62
查看次数

捕获不同方法引发的相同异常?

看看下面的代码,其中两个方法抛出相同类型的异常但完全具有不同的上下文.

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)

c# exception

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

如何在java中修剪运行时变量的空格?

我有以下一段代码

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)

java string

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

GridBagLayout如何工作?

我是主题的新手GridBagLayout,我无法理解约束weight和之间的确切区别fill.

我可以fill不指定weight.

除非您为weightx或weighty指定至少一个非零值,否则所有组件在其容器的中心聚集在一起.这是因为当权重为0.0(默认值)时,GridBagLayout会在其单元格网格与容器边缘之间放置任何额外空间.

我的问题是,如果这是真的那么为什么组件之间没有空间而且它们看起来是附加的?

java layout swing layout-manager gridbaglayout

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

如何刷新JFrame中包含的按钮的颜色?

我正在编写一个小游戏,其中我在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)

java swing

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

如何在c中动态分配静态存储?

在一个递归方法来分组树的外部节点,我需要在该函数内动态分配内存.但是语句

static node* ext_node = malloc (sizeof(node)); 
Run Code Online (Sandbox Code Playgroud)

不起作用,编译器给出错误,其中说

初始化元素不是常量.

总之,我想问一下,static当动态获取指针指向的内存时,如何在递归调用中使用带有指针的关键字?

我需要这个,因为当需要在列表中添加更多元素时,该方法insert_to_end (node*)将负责为新节点分配存储,因此我可以创建list任何长度,这也是确切的内存要求.

但是如何用c语言实现这一目标呢?

c static memory-management list dynamic

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