gpt4 book ai didi

java - 从数组中获取最后一个元素

转载 作者:行者123 更新时间:2023-11-30 10:53:58 26 4
gpt4 key购买 nike

这个问题很久没解决了。我有一些数据写在 csv 文件中。我想将这些元素写入 String 数组,以便能够在下一节课中使用它们,但数组中的每个元素都等于最后一个元素,我不知道我做错了什么,我需要这方面的帮助。

在这个类中,我从 saved.csv 文件中读取并将其写入字符串数组,接下来我通过将这些元素写回新的 csv 文件来检查此写入 - 检查,但不是在每个文件中获取不同的值我的线和上一条一样。

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;

}

编写元素的类:

public class ObjectRep implements Serializable {

private static final long serialVersionUID = 1L;
public static String name;
public static String color;
private byte []descriptors;
private int cols;
private int rows;
private int elemSize;
private int MatType;
public static float x;
public static float y;
Mat desc;




public ObjectRep(String name,String color, Mat desc, float x, float y){
this.name=name;
this.desc = desc;
MatToBytes(desc);
this.color=color;
this.x = x;
this.y = y;
}



public Byte MatToBytes(Mat data){
cols=data.cols();
rows=data.rows();
elemSize=(int)data.elemSize();
MatType=data.type();
descriptors=new byte[(int)(data.total()*data.elemSize())]; // elemSize is in bytes
data.get(rows, cols, descriptors);
return null; //??
}


public Mat bytesToMat(){
if(descriptors!=null){
Mat desc = new Mat(rows, cols, MatType);
desc.put(0, 0, descriptors);
return desc;
}else return null;
}

保存.csv:

table black /storage/emulated/0/Pictures/picFolder/table_black.jpg 700.0 435.0 blabla blabla /storage/emulated/0/Pictures/picFolder/blabla_blabla.jpg 117.0 348.0 somesome somesome /storage/emulated/0/Pictures/picFolder/somesome_somesome.jpg 641.0 298.0 nanana nanana /storage/emulated/0/Pictures/picFolder/nanana_nanana.jpg 129.0 392.0 something something /storage/emulated/0/Pictures/picFolder/something_something.jpg 299.0 504.0 laptop white /storage/emulated/0/Pictures/picFolder/laptop_white.jpg 404.0 572.0

然后我有了新的 check.csv,它看起来像这样:

laptop white /storage/emulated/0/Pictures/picFolder/laptop_white.jpg 404.0 572.0 laptop white /storage/emulated/0/Pictures/picFolder/laptop_white.jpg 404.0 572.0 laptop white /storage/emulated/0/Pictures/picFolder/laptop_white.jpg 404.0 572.0 laptop white /storage/emulated/0/Pictures/picFolder/laptop_white.jpg 404.0 572.0 laptop white /storage/emulated/0/Pictures/picFolder/laptop_white.jpg 404.0 572.0

感谢您的帮助

最佳答案

你的问题在这里

} else if (j == 5) {

...

objectTab[numer] = new ObjectRep(name2, color2, desc2, xsec, ysec);

...

当您读取文件时,您只会在 j = 5 时加载 objectTab。

关于java - 从数组中获取最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33805718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com