Java 将数组中的最后一个元素作为
我有一个长期无法解决的问题。我有一些数据写在csv文件。我想将这些元素写入字符串数组,以便在下一个类中使用它们,但数组中的每个元素都与最后一个元素相等,我不知道我做错了什么,我需要帮助 在这个类中,我从saved.csv文件中读取并将其写入字符串数组,接下来我将通过将这些元素写入新的csv文件来检查写入情况-检查,但不是在每一行中获得不同的值,而是与上一行相同Java 将数组中的最后一个元素作为,java,android,arrays,Java,Android,Arrays,我有一个长期无法解决的问题。我有一些数据写在csv文件。我想将这些元素写入字符串数组,以便在下一个类中使用它们,但数组中的每个元素都与最后一个元素相等,我不知道我做错了什么,我需要帮助 在这个类中,我从saved.csv文件中读取并将其写入字符串数组,接下来我将通过将这些元素写入新的csv文件来检查写入情况-检查,但不是在每一行中获得不同的值,而是与上一行相同 public class MainActivity extends AppCompatActivity { Button button
public class MainActivity extends AppCompatActivity {
Button button;
Button button2;
Button button3;
DescriptorMatcher matcher;
static String name2;
static String color2;
String x2;
String y2;
float xsec;
float ysec;
Mat desc2;
static String descriptor2;
InputStream fis = null;
String line;
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Objects";
File file = new File(path+"/saved.csv");
File file2 = new File(path+"/check.csv");
int numer = 0;
public static String[] dataToSave = null;
public static ObjectRep[] objectTab = new ObjectRep[25];
static {
if (!OpenCVLoader.initDebug()) {
// Handle initialization error
}
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
{
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: // Callback method, called after OpenCV library initialization
{
FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.ORB); //or BRISK
MatOfKeyPoint keyPoints = new MatOfKeyPoint();
Mat descriptors= new Mat();
// desc2 = new Mat();
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB); //32Bytes per KeyPoint
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT);
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
readAndWrite();
Log.d("Name of 5 " + objectTab[5].name, "x" + objectTab[5].x);
Log.d("Name of 0 "+objectTab[0].name,"x "+objectTab[0].x);
Log.d("Name of 1 "+objectTab[1].name,"x "+objectTab[1].x);
Log.d("Name of 3 "+objectTab[3].name,"x "+objectTab[3].x);
for(int i=0; i<objectTab.length; i++) {
dataToSave = new String[]{objectTab[i].name, objectTab[i].color, objectTab[i].desc.toString(), String.valueOf(objectTab[i].x), String.valueOf(objectTab[i].y)};
save(file2, dataToSave);
}
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button.setFocusable(true);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),AddingObject.class);
MainActivity.this.startActivity(i);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),LookObject.class);
MainActivity.this.startActivity(intent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
}
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void readAndWrite() {
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (fis != null) {
// prepare the file for reading
InputStreamReader chapterReader = new InputStreamReader(fis);
BufferedReader buffreader = new BufferedReader(chapterReader);
int j = 1;
// read every line of the file into the line-variable, on line at the time
for (int i = 0; i < 150; i++, j++) {
// do {
line = buffreader.readLine();
if (j == 1) {
name2 = line;
} else if (j == 2) {
color2 = line;
} else if (j == 3) {
descriptor2 = line;
File image = new File(descriptor2);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
desc2 = new Mat(bitmap.getHeight(), bitmap.getWidth(), CvType.CV_8UC1);
Utils.bitmapToMat(bitmap, desc2);
Imgproc.cvtColor(desc2, desc2, Imgproc.COLOR_RGB2GRAY);
//desc2 = Imgcodecs.imread(descriptor2);
} else if (j == 4) {
x2 = line;
xsec = Float.valueOf(x2);
} else if (j == 5) {
y2 = line;
ysec = Float.valueOf(y2);
j = 0;
objectTab[numer] = null;
objectTab[numer] = new ObjectRep(name2, color2, desc2, xsec, ysec);
Log.d("Przedmiot" + objectTab[numer].name, " ");
if(i==5){
Log.d("Przedmiot i=5" + objectTab[5].name, " ");
}
clear();
if (objectTab[numer] != null) {
Toast.makeText(getApplicationContext(), "Object added ", Toast.LENGTH_SHORT).show();
}
numer++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "adding failed", Toast.LENGTH_SHORT).show();
} finally {
// close the file.
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void save(File file, String[] data)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(file, true);
}
catch (FileNotFoundException e) {e.printStackTrace();}
try
{
try
{
BufferedReader br = new BufferedReader(new FileReader(file));
if (br.readLine() != null) {
fos.write("\n".getBytes());
}
for (int i = 0; i<data.length; i++)
{
fos.write(data[i].getBytes());
if (i < data.length-1)
{
fos.write("\n".getBytes());
}
}
}
catch (IOException e) {e.printStackTrace();}
}
finally
{
try
{
fos.close();
}
catch (IOException e) {e.printStackTrace();}
}
}
public void clear(){
name2 = null;
color2 = null;
x2 = null;
y2 = null;
xsec = 0;
ysec = 0;
descriptor2 = null;
desc2 = null;
}
}
saved.csv:
table black/storage/simulated/0/Pictures/picFolder/table_black.jpg
700
435.0 blabla bla/storage/simulated/0/Pictures/picFolder/blabla_blabla.jpg
117
348.0 somesome/storage/simulated/0/Pictures/picFolder/somesome\u somesome.jpg
641
298.0 nanana nanana/storage/simulated/0/Pictures/picFolder/nanana_nanana.jpg
129
392.0something/storage/emulated/0/Pictures/picFolder/something\u something.jpg
299
504.0笔记本电脑白色/存储/模拟/0/Pictures/picFolder/laptop_white.jpg
404
572.0
之后,我有了一个新的check.csv,如下所示:
笔记本电脑白色/存储/模拟/0/Pictures/picFolder/laptop_white.jpg
404
572.0笔记本电脑白色/存储/模拟/0/Pictures/picFolder/laptop_white.jpg
404
572.0笔记本电脑白色/存储/模拟/0/Pictures/picFolder/laptop_white.jpg
404
572.0笔记本电脑白色/存储/模拟/0/Pictures/picFolder/laptop_white.jpg
404
572.0笔记本电脑白色/存储/模拟/0/Pictures/picFolder/laptop_white.jpg
404
572.0
非常感谢您的帮助您的问题就在这里
} else if (j == 5) {
...
objectTab[numer] = new ObjectRep(name2, color2, desc2, xsec, ysec);
...
当您读取文件时,仅当j=5时加载objectTab您会打印什么程序?在Log.d中(“Nazwa 5”+objectTab[5].name,“x”+objectTab[5].x);Log.d(“Nazwa 0”+objectTab[0].name,“x”+objectTab[0].x);Log.d(“Nazwa 1”+objectTab[1].name,“x”+objectTab[1].x);Log.d(“Nazwa 3”+objectTab[3].name,“x”+objectTab[3].x);哦,对不起,这是对第5、0、1和3d元素的随机检查,以确定名称和x,但在每个“i”中,我得到的是“laptop”作为名称,“404.0”作为x,而不是其他值。在这个过程中,您的日志输出是什么?我想没关系,因为我调试了它,当j=5时,每一行都被读取,然后我可以创建这个对象。对象已正确创建,但使用此方法后,当我要将此数组写入新文件时,它只会看到最后一个对象并覆盖到每个对象。我认为您的源文件与您在问题中显示的源文件不同。因为根据逻辑,yuo每行只有一个值。是的,这是csv文件,每行的值不同,但模式相同:1)名称2)颜色3)jpg文件路径4)x 5)并且最终文件只有一个元素?它是白色的笔记本电脑?我发现这是因为在ObjectRep中有两个类似于Vrable的静态公共静态字符串名称;公共静态字符串颜色;使用static时,该变量只有一个实例。如果您删除了静态文件,您的程序将正常工作
} else if (j == 5) {
...
objectTab[numer] = new ObjectRep(name2, color2, desc2, xsec, ysec);
...