我需要得到一个具有3种类型值的变量类型,我知道这样做的唯一方法是"记录",但我知道Java中没有类似"记录"的类型ArrayList,但我不明白...
我的结果应该是:(不知道它在Java中看起来如何,所以我以不同的风格展示)
TData = Record
Username : String;
UserID : Int ;
RowID : Int;
end;
users : array[1..10] of TData;
for(int i = 1; i < rowCount; i++){
TData.Username[i] = rs.GetString("Name");
TData.UserID[i] = rs.GetInt("UserID");
TData.RowID[i] = row;
}
Run Code Online (Sandbox Code Playgroud)
关于如何制作这样的东西的任何建议?是ArrayList我真正需要的?
感谢您的帮助,最后我结合并得到了这个结果,完美地运作:
class MyData {
private String userName;
private int userID;
private int RowID;
public MyData(String userName, int userID, int RowID) {
this.userName = userName;
this.userID = userID;
this.RowID = RowID;
}
public String getUserName() {
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个程序,它接受一个庞大的数据集,然后使用它运行一些查询mapreduce.我有这样的代码:
public static class MRMapper
extends Mapper<LongWritable, Text, Text, IntWritable>{
String output2="hdfs://master:9000/user/xxxx/indexln.txt";
FileSystem Phdfs =FileSystem.get(new Configuration());
Path fname1=new Path(output2);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(Phdfs.create(fname1,true)));
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
long max=0;
public void map(LongWritable key, Text value, Context context
) throws IOException, InterruptedException {
String binln = Long.toBinaryString(0x8000000000000000L | key).substring(1);
out2.write(binln+"\n");
out2.flush();
String line = value.toString();
String [] ST = line.split(",");
long val=Math.abs(Long.parseLong(ST[2]));
if (max < val){
max= val;
} …Run Code Online (Sandbox Code Playgroud) 我找到了一些博客,其中提出了new在创建类的对象时避免使用关键字的建议.创建没有new关键字的对象的一些示例是-
SampleObject obj = Class.forName("com.example.SampleObject").newInstance();
Run Code Online (Sandbox Code Playgroud)
或使用clone()方法 -
SampleObject obj1 = new SampleObject();
SampleObject obj2 = obj.clone();
Run Code Online (Sandbox Code Playgroud)
然后在这里我找到了一些没有new关键字创建对象的好例子
我可以理解'Factory'模式的优点,同时避免new代码主要部分的关键字.如果我没有使用任何设计模式(用于创建对象),那么创建没有new关键字的对象有什么好处?或者没有创建对象的原因是什么new?
假设我们在全局命名空间中声明了以下lambda:
auto Less = [](int a,int b) -> bool
{
return a < b;
}
Run Code Online (Sandbox Code Playgroud)
以下代码使用了这个lambda:
template<typename T>
struct foo
{
foo(int v){}
bool operator<(const foo<T>&) const
{
return T(1,2);
}
};
int main()
{
typedef foo<decltype(Less)> be_less;
priority_queue<be_less> data;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我将其Less用作结构的模板参数foo.使用g ++ 4.9.2,此代码无法编译:
test1.cpp:13:21: error: no matching function for call to '<lambda(int, int)>::__lambda0(int, int)'
return T(1,2);
^
test1.cpp:13:21: note: candidates are:
test1.cpp:17:14: note: constexpr<lambda(int, int)>::<lambda>(const<lambda(int, int)>&)
auto Less = [](int a,int b) -> bool
^ …Run Code Online (Sandbox Code Playgroud) 我正在努力java-8.请参阅以下代码段 -
studentsOfThisDept = students.stream()
.filter(s -> (student != null
&& s.getDepartment() != null
&& s.getDepartment().getCode().equals("CS")
))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
在这里我要进行2次检查 -
s.getDepartment() != null ; // 1st check
Run Code Online (Sandbox Code Playgroud)
和
s.getDepartment().getCode().equals("CS") // 2nd check
Run Code Online (Sandbox Code Playgroud)
有什么方法可以存储s.getDepartment()一些变量的值(比如说dept),这样在第二次检查时我可以写 -
dept.getCode().equals("CS");
Run Code Online (Sandbox Code Playgroud) 我有一个名为responseObj 的地图
Map<String, object> responseObj = new HashMap<String, Object>();
Run Code Online (Sandbox Code Playgroud)
我在responseObj中添加了一些值,如下所示 -
responseObj.put("canApprove", true); //1
responseObj.put("approvers", userList);
Run Code Online (Sandbox Code Playgroud)
这userList是一个列表或用户 -
List<User> userList = new ArrayList<User>();
Run Code Online (Sandbox Code Playgroud)
User 类中的用户对象 -
class User {
private int userId;
private int roleId;
}
Run Code Online (Sandbox Code Playgroud)
现在我有一些问题——
是否可以将 转换responseObj为 json 以便"canApprove"成为"approvers"json 的键(请参阅下面的代码片段)以及如何制作它?
{
"canApprove" : true,
"approver": [
{
"userId": 309,
"roleId": 2009
},
{
"userId": 3008,
"roleId": 2009
},
Run Code Online (Sandbox Code Playgroud)
] }
如果我将其转换为 json 并从 jsp 获取响应,我是否可以在 jsp 中获取相应的布尔值,其中引用"canApprove"an ? …
我正在开发一个应用程序,我必须处理自定义变量aVar-
String aVar = price/barcode/area1/true // varName/varType/varScope/tracable
Run Code Online (Sandbox Code Playgroud)
可能还添加了一些其他属性,aVar并用(“/”)分隔。要获得varName, varType, varScope等,我必须执行以下操作,请参阅下面的代码 -
String[] token = aVar.split("/");
String varName = token[0];
String varType = token[1];
String varScope = token[2];
String traceable = token[3];
Run Code Online (Sandbox Code Playgroud)
在这里您可以看到varName取自token[0],取自'条形码price'等等。这里我假设 - 分割后始终位于 token[0] 中,始终位于 token[1] 中,依此类推。现在我的问题是 -该方法返回的数组是否始终按照它们出现的顺序包含令牌(价格-->条形码-->area1-->true)?varTypetoken[1]varNamevarTypeStringsplit()String
我已经用一些输入对此进行了多次测试,发现顺序保持不变。但我不确定对于很长的字符串来说它是否总是正确的。
请问smb请解释String按字母顺序排序字符的过程?例如,如果我有String "hello"输出应该"ehllo"但我的代码做错了.
public static void main(String[] args)
{
String result = "";
Scanner kbd = new Scanner(System.in);
String input = kbd.nextLine();
for(int i = 1; i < input.length(); i++)
{
if(input.charAt(i-1) < input.charAt(i))
result += input.charAt(i-1);
//else
// result += input.charAt(i);
}
System.out.println(result);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在构建我的第一个游戏,在Java网站上严重依赖各种教程和指南,我遇到了一个问题.在我的游戏引擎中,我想调用该Player.update()方法,但它说它必须是static(不能对非静态方法进行静态引用)但是,调用它的方法不是static.任何人都可以告诉我为什么它需要它static?它不需要Update中唯一的其他方法static.
package Main;
import java.awt.*;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import Graphics.Assets;
import Sprites.Player;
@SuppressWarnings("serial")
public class Game extends JPanel
implements Runnable, KeyListener{
//TEST CODE
private int x = 0;
private int y = 0;
private int dY = 1;
private int dX = 1;
public void moveBall() {
x = x + dX;
y = y + dY;
if(x > WIDTH - 28) { …Run Code Online (Sandbox Code Playgroud) 下面是我的POJO课程,其中有50个带有setter和getter的字段。
Class Employee{
int m1;
int m2;
int m3;
.
.
int m50;
//setters and getters
Run Code Online (Sandbox Code Playgroud)
在另一堂课中,我需要获得所有这50个字段的总和
Employee e1 =new Emploee();
int total = e1.getM1()+e2.getM2()+........e2.getM50();
Run Code Online (Sandbox Code Playgroud)
无需手动进行50条记录,而是可以通过任何循环动态地进行操作。
谢谢