你能告诉我如何在java中添加数组的前三个位置,无论数组的大小是多少?

时间:2021-09-07 01:10:06

basically my question is how can I just add the first three places of an array of size 5 and access them. I have been trying, but I just can't get it.

基本上我的问题是如何才能添加大小为5的数组的前三个位置并访问它们。我一直在努力,但我无法得到它。

There is an array of students and i want to remove some elements of the array and print the rest:

有一系列的学生,我想删除数组的一些元素,并打印其余的:

Students[] temp = new Students[(sArray.length-1)];
int j = 0;

for(int i = 0; i < sArray.length; i++){
  if (i != value ){
    temp[j] = sArray[i];
    j++;
  }

  sArray = temp;
  count = count-1;
}

for(int i = 0; i < getCount(); i++){
  // this will just print, but it is not printing, 
  // and count is the no of students.
  result+= sArray[i].toString()+"\n";
  result+="\n";
}

2 个解决方案

#1


0  

It is hard to understand what you are asking, but my best guess is that you want to expand an array. The only way to do this in Java is to create a new array; e.g.

很难理解你在问什么,但我最好的猜测是你要扩展一个数组。在Java中执行此操作的唯一方法是创建一个新数组;例如

    int[] small = new int[]{1, 2, 3};
    ...
    int[] big = Arrays.copyOf(small, 5);
    // The 'big' array will have 5 elements, and the first 3 will
    // be the elements of 'old'.

Note that you cannot change the size of an array in Java. The length is an immutable property of the array.

请注意,您无法在Java中更改数组的大小。长度是数组的不可变属性。

#2


0  

You seem to be saying that you want to print all the elements of an Array even if the Array is not full. The proper class to do this is really an ArrayList, which will expand and contract to your needs as you fill it (thus, it is always "full" and scales as things are added).

你似乎在说你要打印数组的所有元素,即使数组未满。正确的类是一个ArrayList,它会在你填充它时扩展和收缩你的需求(因此,它总是“满”并随着事物的增加而扩展)。

http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

If you really insist on using an Array, you should have given us more info/code so we know exactly what you are trying to do. Having some code is a lot better than trying to explain using vague terms. Anyways, for an Array of Objects, you can do

如果你真的坚持使用数组,你应该给我们更多的信息/代码,这样我们就知道你想要做什么。有些代码比使用模糊术语解释要好得多。无论如何,对于一个对象阵列,你可以做到

for(int i = 0; i < myArray.length; i++)
{
    if(myArray[i] != null)
    {
        System.out.println(myArray[i]);
    }
}

And for ints, since they are initialized to 0, you check against 0:

对于int,因为它们被初始化为0,所以你检查0:

for(int i = 0; i < myArray.length; i++)
{
    if(myArray[i] != 0)
    {
        System.out.println(myArray[i]);
    }
}

#1


0  

It is hard to understand what you are asking, but my best guess is that you want to expand an array. The only way to do this in Java is to create a new array; e.g.

很难理解你在问什么,但我最好的猜测是你要扩展一个数组。在Java中执行此操作的唯一方法是创建一个新数组;例如

    int[] small = new int[]{1, 2, 3};
    ...
    int[] big = Arrays.copyOf(small, 5);
    // The 'big' array will have 5 elements, and the first 3 will
    // be the elements of 'old'.

Note that you cannot change the size of an array in Java. The length is an immutable property of the array.

请注意,您无法在Java中更改数组的大小。长度是数组的不可变属性。

#2


0  

You seem to be saying that you want to print all the elements of an Array even if the Array is not full. The proper class to do this is really an ArrayList, which will expand and contract to your needs as you fill it (thus, it is always "full" and scales as things are added).

你似乎在说你要打印数组的所有元素,即使数组未满。正确的类是一个ArrayList,它会在你填充它时扩展和收缩你的需求(因此,它总是“满”并随着事物的增加而扩展)。

http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

If you really insist on using an Array, you should have given us more info/code so we know exactly what you are trying to do. Having some code is a lot better than trying to explain using vague terms. Anyways, for an Array of Objects, you can do

如果你真的坚持使用数组,你应该给我们更多的信息/代码,这样我们就知道你想要做什么。有些代码比使用模糊术语解释要好得多。无论如何,对于一个对象阵列,你可以做到

for(int i = 0; i < myArray.length; i++)
{
    if(myArray[i] != null)
    {
        System.out.println(myArray[i]);
    }
}

And for ints, since they are initialized to 0, you check against 0:

对于int,因为它们被初始化为0,所以你检查0:

for(int i = 0; i < myArray.length; i++)
{
    if(myArray[i] != 0)
    {
        System.out.println(myArray[i]);
    }
}