我有一个组件类:
public class Components {
int numberOfNets;
String nameOfComp;
String nameOfCompPart;
int numOfPin;
public components(int i, String compName, String partName, int pin) {
this.numberOfNets = i;
this.nameOfComp = compName;
this.nameOfCompPart = partName;
this.numOfPin = pin;
}
}
Run Code Online (Sandbox Code Playgroud)
在另一个类中,我创建了一个 Components 类的数组列表:
List<Components> compList = new ArrayList<Components>();
Run Code Online (Sandbox Code Playgroud)
稍后在代码中,我以这种方式添加 List 中的元素:
compList.add(new Components(0,compName,partName,0));
Run Code Online (Sandbox Code Playgroud)
请参阅此处,numberOfNetsComponentsnumOfPin类中的变量以 0 值启动。但是这些值在代码的后面部分中进行计算/递增,因此我需要更新每个列表元素中这两个变量的新值。现在,从ArrayList 文档中,我得到了通过操作使用索引来更新列表元素的想法set。但我很困惑如何在类的 ArrayList 中设置/更新类的特定变量。我只需要更新这两个提到的变量,而不是 Components 类中的所有四个变量。有什么办法可以做到这一点吗?
我的问题是 -
给定两个相等长度的字符串s1和s2作为输入,预期输出是一个字符串,其中第一个字符来自s1,然后第一个字符来自s2,然后第二个字符来自s1,然后第二个字符来自s2,依此类推.例如,如果s1 ="Outer",s2 ="Space",则输出为"OSuptaecre".
我已经完成了循环,我想知道是否有任何其他简单的代码用于此程序,如使用字符串函数.
public class JoinChars {
static String testcase1 = "Right";
static String testcase2 = "Wrong";
public static void main(String args[]){
JoinChars testInstance= new JoinChars();
String result = testInstance.join(testcase1,testcase2);
System.out.println(result);
}
public String join(String str1, String str2){
String str3="";
if(str1.length()>=str2.length()){
for(int i=0;i<str1.length();i++){
str3+=str1.charAt(i);
for(int j=i;j<str2.length();){
str3+=str2.charAt(j);
break;
}
}
}
else if(str2.length()>=str1.length()){
for(int i=0;i<str2.length();i++){
str3+=str2.charAt(i);
for(int j=i;j<str1.length();){
str3+=str1.charAt(j);
break;
}
}
}
return str3;
}
}
Run Code Online (Sandbox Code Playgroud) 我在 PySpark 中有一个包含 3 列的数据框 - json、date 和 object_id:
-----------------------------------------------------------------------------------------
|json |date |object_id|
-----------------------------------------------------------------------------------------
|{'a':{'b':0,'c':{'50':0.005,'60':0,'100':0},'d':0.01,'e':0,'f':2}}|2020-08-01|xyz123 |
|{'a':{'m':0,'n':{'50':0.005,'60':0,'100':0},'d':0.01,'e':0,'f':2}}|2020-08-02|xyz123 |
|{'g':{'h':0,'j':{'50':0.005,'80':0,'100':0},'d':0.02}} |2020-08-03|xyz123 |
-----------------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
现在我有一个变量列表:[ac60, an60, ad, gh]。我只需要从上述数据帧的 json 列中提取这些变量,并将这些变量添加为数据帧中具有各自值的列。
所以最后,数据框应该是这样的:
-------------------------------------------------------------------------------------------------------
|json |date |object_id|a.c.60|a.n.60|a.d |g.h|
-------------------------------------------------------------------------------------------------------
|{'a':{'b':0,'c':{'50':0.005,'60':0,'100':0},'d':0.01,...|2020-08-01|xyz123 |0 |null |0.01|null|
|{'a':{'m':0,'n':{'50':0.005,'60':0,'100':0},'d':0.01,...|2020-08-02|xyz123 |null |0 |0.01|null|
|{'g':{'h':0,'j':{'k':0.005,'':0,'100':0},'d':0.01}} |2020-08-03|xyz123 |null |null |0.02|0 |
-------------------------------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
请帮助获取此结果数据框。我面临的主要问题是由于传入的 json 数据没有固定的结构。json 数据可以是嵌套形式的任何内容,但我只需要提取给定的四个变量。我在 Pandas 中通过展平 json 字符串然后提取 4 个变量来实现这一点,但在 Spark 中它变得越来越困难。