Array definition, value and two-dimensional array interview case

Today, I suddenly received an interview question at work ~ I plan to give it a try. I found that some of the knowledge points are relatively lacking.

Picture above

This is an interview question

My code

import java.util.List;

import org.hibernate.loader.plan.spi.ScalarReturn;

public class test {

	public static void main(String[] args) {
		Integer[][]ar={{0,0,1,1,0},{0,1,1,1,0},{0,1,1,1,0},{0,1,1,1,1},{1,0,0,0,0}}; 
		String get=find(ar,2,2);
		System.out.println(get);
	}
	//实现方法
	public static String find(Integer[][]arr,int x,int y){
		String str="该("+x+","+y+")坐标下兄弟元素为:";
	 
		if(arr[x-1][y].equals(1)){
			str  +="("+(x-1)+","+y+")";
		}
		if(arr[x+1][y].equals(1)){
			str  +="("+(x+1)+","+y+")";
		}
		if(arr[x][y+1].equals(1)){
			str  +="("+x+","+(y+1)+")";
		}
		if(arr[x][y-1].equals(1)){
			str  +="("+x+","+(y-1)+")";
		}
		return str;
		
	}
}

I feel that this method is very common and not high enough, but it does achieve the results; it
mainly uses the definition of the two-dimensional array and obtains the corresponding coordinate elements. Conduct introspection.

One-dimensional array

Array definition:

The first way:
String [] arr = new String [3];
// Assign the array value
arr [0] = “3”;
arr [1] = “1”;
arr [2] = “3”;

The second way:
String [] arr2 = {"3", "1", "2"};

The third way:
String [] arr3 = new String [] {“1”, “2”, “3”};

Get array:

String a = arr [subscript];

Two-dimensional array

Array definition

The first way:
int [] [] arr = new int [3] [4] // 3 rows and 4 columns of three arrays with four elements in each group
The second way
int [] [] arr2 = new int [3] []; The
third way
int [] [] arr3 = {{1,2}, {2,3}, {3.4}, {4,2}}; // Define four arrays with two elements in each group

Get array elements

int a = arr [2] [1] // According to the element index

Published 4 original articles · received 1 · views 485

Guess you like

Origin blog.csdn.net/weixin_44760375/article/details/105631184