我有一个算法,从stdout读取一个名为'name'的字符串变量,然后创建一个存储名称的字符串数组..我试图在java中这样做,但首先,我不知道如何将名称传递给数组.在C或C++中,我可以轻松地完成这个:strcpy(arr,name); 但这些是字符串类型,并且我已经看到Java中没有等效的c_str().拜托,我该怎么做?
.... //codes are here...
System.out.Println("enter your name and press enter:");
BufferedReader br = new BufferedReader(InputStreamReader(System.in));
String name = null;
try
{
name = br.readLine();
String[] arr = new String[name.length];
//wish this was C++;
strcpy(arr.c_str(), name.c_str()); //how do you copy the name string to the arr
//string?
}catch(IOException e)
{
System.out.Println(e.getMessage());
}
MyClass A = new MyClass(arr);
Run Code Online (Sandbox Code Playgroud) 拜托,也许,我太累了因为我似乎无法弄清楚为什么这个输入流操作符重载函数正在抱怨..这是.h声明:
friend std::istream& operator>>(std::istream& in, IntOperatorClass& intoperatorClass)
Run Code Online (Sandbox Code Playgroud)
这是.cpp声明:
std::istream& operator >>(std::istream& in, IntOperatorClass& intoperatorClass)
{
in>>intoperatorClass.value;
return in;
}
Run Code Online (Sandbox Code Playgroud)
该类有一个名为value的私有变量..
班级 :
class IntOperatorClass{
int value; //a value
int arr[10];//an array value
public:
IntOperatorClass();//default constructor
IntOperatorClass(int);//constructor with parameter
IntOperatorClass(int arr[], int length);//an array constructor
IntOperatorClass(const IntOperatorClass& intoperatorClass);//copy constructor
IntOperatorClass& operator=(const IntOperatorClass& intoperatorClass);//assignment operator
friend std::ostream& operator <<(std::ostream& out, const IntOperatorClass& intoperatorClass);//output operator;
friend std::istream& operator>>(std::istream& in, IntOperatorClass& intoperatorClass);//input operator
IntOperatorClass& operator++();//pre increment operator
IntOperatorClass operator++(int);//post increment operator
friend IntOperatorClass operator+(const …Run Code Online (Sandbox Code Playgroud) 在codility排列检查问题中:
A non-empty zero-indexed array A consisting of N integers is given.
A permutation is a sequence containing each element from 1 to N once, and only once.
For example, array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
A[3] = 2
is a permutation, but array A such that:
A[0] = 4
A[1] = 1
A[2] = 3
is not a permutation.
The goal is to check whether array A is a permutation.
Write a …Run Code Online (Sandbox Code Playgroud) 我只是想知道为什么这段代码会抛出错误.错误是:
"线程中的异常"Thread-1"java.lang.Error"
class Salmon extends Thread
{
public static long id;
public void run()
{
for(int i = 0;i<4; i++){
if(i==2&& id ==Thread.currentThread().getId()){
//if(i==2){
new Thread(new Salmon()).start();
throw new Error();
}
System.out.println(i + " ");
}
}
public static void main(String[] args)
{
Thread t1 = new Salmon();
id = t1.getId();
t1.start();
}
}
Run Code Online (Sandbox Code Playgroud) 下面我使用这个脚本来设置文本框值..
$('#sub').click(function(){
var num1 = $('#text1').val();
var num2 = $('#text2').val();
var newVal = (num1+2)*(num2/4);
var nextVal =(num1+4)*(num2/2);
$(#text1).val(newVal); //setting the textbox value here
Run Code Online (Sandbox Code Playgroud)