我对将这一个对象复制到另一个对象的这两种方法有点新鲜.我很困惑,无法发现深拷贝和浅拷贝之间的主要区别..我已经经历了很多关于这个的理论,但我需要用适当的例子解释..我有一个程序,我将一个对象复制到另一个. - >
class A
{
public int a = 0;
public void display()
{
Console.WriteLine("The value of a is " + a);
}
}
class Program
{
static void Main(string[] args)
{
A ob1 = new A();
ob1.a = 10;
ob1.display();
A ob2 = new A();
ob2 = ob1;
ob2.display();
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
这是浅拷贝还是深拷贝?任何人都可以请原因提供答案.如果是深拷贝,那么请为此程序提供浅拷贝的代码,执行相同的对象复制工作,反之亦然.
如果以上是浅拷贝,那么即使这应该是浅拷贝 - >
A ob1 = new A();
ob1.a = 10;
ob1.display();
A ob2 = ob1;
ob2.a = 444;
ob1.display();
Run Code Online (Sandbox Code Playgroud) 我对c#有点新,我在这一点上卡住了,我有一个常规的字符串,我用它\
来逃避"
,逃到这里意味着逃避编译器的解释"
,并"
在屏幕上打印,而我得到预期的输出 - >
class Program
{
static void Main(string[] args)
{
string s1 = "This is a \"regular\" string";
System.Console.WriteLine(s1);
System.Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一个逐字字符串,我试图以与上面相同的方式逃避"
使用\
..-->
class Program
{
static void Main(string[] args)
{
string s2 = @"This is \t a \"verbatim\" string";//this would escape \t
System.Console.WriteLine(s2);
System.Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么以上不起作用?
我有两个A和B类,其中B来自A.
这两个类都有一个具有相同签名的方法.它们在Java&c# - >中以下列方式调用
在JAVA的情况下:
class A
{
public void print()
{
System.out.println("Inside Parent");
}
}
class B extends A
{
public void print()
{
System.out.println("Inside Child");
}
}
class test4
{
public static void main(String args[])
{
B b1=new B();
b1.print();
A a1=new B();
a1.print();
}
}
Run Code Online (Sandbox Code Playgroud)
该程序生成以下输出: -
Inside Child
Inside Child
Run Code Online (Sandbox Code Playgroud)
在C#的情况下:
class A
{
public void print()
{
System.Console.WriteLine("Inside Parent");
}
}
class B : A
{
public void print()
{
System.Console.WriteLine("Inside Child");
}
}
class …
Run Code Online (Sandbox Code Playgroud) 考虑我有一个包含3个语句的try块,所有这些语句都会导致异常.我希望所有3个例外都由它们相关的catch块处理..是否可能?
像这样的东西 - >
class multicatch
{
public static void main(String[] args)
{
int[] c={1};
String s="this is a false integer";
try
{
int x=5/args.length;
c[10]=12;
int y=Integer.parseInt(s);
}
catch(ArithmeticException ae)
{
System.out.println("Cannot divide a number by zero.");
}
catch(ArrayIndexOutOfBoundsException abe)
{
System.out.println("This array index is not accessible.");
}
catch(NumberFormatException nfe)
{
System.out.println("Cannot parse a non-integer string.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
是否有可能获得以下输出? - >>
Cannot divide a number by zero.
This array index is not accessible.
Cannot parse a non-integer string.
Run Code Online (Sandbox Code Playgroud) 我发现了几个类似于此的帖子,但是找不到一个能够解释这个问题的答案.我已经执行了一个类的嵌套,即类'inside'存在于类'outside'中并尝试实例化内部这是我遇到的情景
在C#的情况下:
class outside
{
public class inside
{
public void print()
{
System.Console.WriteLine("I am inside");
}
}
}
class Program
{
public static void Main(string[] args)
{
/* here i am trying to instantiate the inner class 'inside' */
outside.inside obj = new outside.inside();
obj.print();
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
我在里面所以,上面的工作正常.但是,
在Java的情况下:
class outside
{
public class inside
{
public void print()
{
System.out.println("I am inside");
}
}
}
class Demo
{
public static void main(String[] args)
{
/* here …
Run Code Online (Sandbox Code Playgroud) 我已经看过很多关于通过更改其返回类型来重载方法的帖子,但是,理想情况下,以下程序应该可以正常工作,因为i
integer类型的变量只能包含整数值.
因此,它理想情况下应该调用函数int print(int a)
函数,甚至不看函数,float print(int a)
因为它返回一个浮点值,而且main()
,我使用了一个整数变量来保存方法返回的值,而整数变量永远不能保持浮点数价值..
以下代码演示了它→
class temp
{
public float print(int a)
{
int l=12.55;
return l;
}
public int print(int a)
{
int p=5;
return p;
}
}
class Program
{
static void Main(string[] args)
{
temp t=new temp();
int i=t.print(10);
A.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
在其他情况下,我做这样的事情→
class temp
{
public float print(int a)
{
int l=12.55;
return l;
}
public int print(int a)
{
int p=5;
return p;
}
} …
Run Code Online (Sandbox Code Playgroud) 在一次面试中,我被要求编写一个 Java 程序来合并 K 个排序数组。输入如下:
int[][] kSortedArrs = { {1, 5, 6, 8},
{2, 4, 10, 12},
{3, 7, 9, 11},
{13, 14, 15, 16}} ;
Run Code Online (Sandbox Code Playgroud)
我知道合并两个排序数组的解决方案,因此我编写了该方法(mergeSortedArrs)并反复使用它作为该问题的解决方案。代码如下所示:
public static int[] mergeKSortedArrays(int[][] arr) {
int[] mergedArr = new int[] {};
for(int i=0; i < arr.length; i++) {
mergedArr = mergeSortedArrs(mergedArr, arr[i]);
}
return mergedArr;
}
private static int[] mergeSortedArrs(int[] arr1, int[] arr2) {
int[] arr3 = new int[arr1.length + arr2.length];
int i = 0;
int j = 0;
for (int k …
Run Code Online (Sandbox Code Playgroud) java arrays algorithm multidimensional-array data-structures