我正在建立一个堆栈,但我不知道为什么当我试图推动我得到:"无法读取属性'推送'未定义"。
function stack(n) {
return {
size: n,
elements: [],
push: (item) => {
if ( this.elements.length < this.size) {
this.elements.push(item);
return this.elements;
} else {
return this.elements.length !== n ? true : 'Stack Overflow';
}
},
pop: () => {
if (n !== 0) {
return n !== 0 ? this.elements.pop() : 'Stack Underflow';
}
},
isEmpty: () => {
return this.elements === undefined;
},
isFull: () => {
return elements.length === n ? true : false;
},
};
};
发生了什么事?非常感谢。
答案 0 :(得分:0)
在下面的代码中,如果this.elements === undefined,则尝试this.elements.push(1)。在使用.push()之前,需要实例化this.elements。
if (this.elements === undefined || this.elements.length < this.size) {
this.elements.push(1);
return this.elements;
我真的不明白你要做什么,但你需要做这样的事情
if (this.elements === undefined || this.elements.length < this.size) {
this.elements = <instantiate it here>;
this.elements.push(1);
return this.elements;
答案 1 :(得分:0)
我修改了你的代码,它运行正常。基本上它在大小为n的堆栈中,n =数字。然后它执行相对于大小n的方法。
public class ProfileActivity extends AppCompatActivity {
ImageView iv_dp;
boolean checkpermission ;
File file;
Uri uri;
Intent CamIntent, GalIntent, CropIntent ;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_activity);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
allowpermission();
/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
1);
checkpermission = true;
}*/
iv_dp = (ImageView) findViewById(R.id.dp_iv);
iv_dp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(ProfileActivity.this);
alertbox.setTitle("Select an option")
.setItems(R.array.alert, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int pos) {
//pos will give the selected item position
if (pos == 0) {
//cameraoption();
ClickImageFromCamera() ;
} else if (pos == 1) {
GetImageFromGallery();
}
}
});
alertbox.show();
}
});
}
public void ClickImageFromCamera() {
CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(),
"file" + String.valueOf(System.currentTimeMillis()) + ".jpg");
uri = Uri.fromFile(file);
CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
CamIntent.putExtra("return-data", true);
startActivityForResult(CamIntent, 0);
}
public void GetImageFromGallery(){
GalIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(GalIntent, "Select Image From Gallery"), 2);
}
private void allowpermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
1);
checkpermission = true;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
ImageCropFunction();
}
else if (requestCode == 2) {
if (data != null) {
uri = data.getData();
ImageCropFunction();
}
}
else if (requestCode == 1) {
if (data != null) {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
iv_dp.setImageBitmap(bitmap);
}
}
}
public void ImageCropFunction() {
// Image Crop Code
try {
CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(uri, "image/*");
CropIntent.putExtra("crop", "true");
CropIntent.putExtra("outputX", 180);
CropIntent.putExtra("outputY", 180);
CropIntent.putExtra("aspectX", 1);
CropIntent.putExtra("aspectY", 1);
CropIntent.putExtra("scaleUpIfNeeded", true);
CropIntent.putExtra("return-data", true);
startActivityForResult(CropIntent, 1);
} catch (ActivityNotFoundException e) {
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "All set for your Profile Picture", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "No Camera", Toast.LENGTH_SHORT).show();
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
My XML is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@drawable/pro_image"
android:layout_gravity="center"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:id="@+id/dp_iv" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_gravity="center"
android:paddingLeft="20dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:src="@drawable/smile_icon"
android:id="@+id/imageView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UserName"
android:gravity="center"
android:layout_margin="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingLeft="20sp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:src="@drawable/dob_icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date Of Birth"
android:layout_gravity="center"
android:layout_margin="20dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#AAAAAA"
/>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_height="wrap_content">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:src="@drawable/fav_icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Favouries"
android:id="@+id/fav_tv"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#AAAAAA"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:layout_gravity="center"
android:src="@drawable/review_icon1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Reviews"
android:id="@+id/review_tv"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#AAAAAA"
/>
</LinearLayout>
</LinearLayout>
Please help me in my issues over quality of images. Thanks in advance.
使用大小初始化您的堆栈。
function stack(n) {
return {
size: n,
elements: [],
push: function(item){
if (this.elements.length < this.size) {
this.elements.push(1);
return this.elements;
} else {
return 'Stack Overflow';
}
},
pop: function(){
return (this.size !== 0 && this.elements.length) ? this.elements.pop() : 'Stack Underflow';
},
isEmpty: function(){
return (this.elements.length < this.size) ? true : false;
},
isFull: function(){
return (this.elements.length == this.size) ? true : false;
},
};
};
现在调用方法来查看它正在做什么。
var init = stack(2);